Skip to content

Commit

Permalink
fix(react-mnt): 🐛 add missing ref to types
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyghiani committed Jan 24, 2024
1 parent d94d63e commit 562643d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
28 changes: 27 additions & 1 deletion packages/react-mnt/src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ describe('mnt', () => {
describe('typing', () => {
it('should accept properties exclusively for the rendered DOM node', () => {
const Button = mnt('button')<ButtonProps>``;
const Text = mnt('label')``;
const Text = mnt(Button).params({ as: 'span' })``;

type ButtonPropsTest = React.ComponentPropsWithRef<typeof Button>;
type TextPropsTest = React.ComponentPropsWithRef<typeof Text>;

//@ts-expect-error
const button = <Button href='' />;
Expand All @@ -179,6 +182,29 @@ describe('mnt', () => {
expect(buttonWithWrongVariant).toBeTruthy();
expect(buttonWithVariant).toBeTruthy();
expect(textAttrLabel).toBeTruthy();

type SupportAs = Expect<MatchesProperty<ButtonPropsTest, 'as'>>;
type SupportCustomProps = Expect<MatchesProperty<ButtonPropsTest, 'variant'>>;
type SupportRef = Expect<MatchesProperty<ButtonPropsTest, 'ref'>>;
type MatchesRef = Expect<Equal<ButtonPropsTest['ref'], React.Ref<HTMLButtonElement>>>;
type SupportExtendedRef = Expect<MatchesProperty<TextPropsTest, 'ref'>>;
type MatchesExtendedRef = Expect<Equal<TextPropsTest['ref'], React.Ref<HTMLSpanElement>>>;

// @ts-ignore
// eslint-disable-next-line no-unused-vars
type Assertions =
| SupportAs
| SupportCustomProps
| SupportRef
| MatchesRef
| SupportExtendedRef
| MatchesExtendedRef;
});
});
});

type Expect<T extends true> = T;
type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;

type MatchesProperty<Type, Key> = Key extends keyof Type ? true : false;
4 changes: 1 addition & 3 deletions packages/react-mnt/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export interface Mnt<TElement extends MntComponentType, TElementHtmlProps extend
export interface MntComponent<
TElement extends MntComponentType = any,
Props extends object = BaseObject
> extends React.ForwardRefExoticComponent<
React.PropsWithoutRef<Props> & React.RefAttributes<unknown> & MntProps
> {
> extends React.ForwardRefExoticComponent<React.PropsWithRef<Props> & MntProps> {
<TAs extends MntComponentType>(_props: MntComponentProps<TAs, Props>): JSX.Element;

_classesFactory: ClassesFactory<Props>;
Expand Down

0 comments on commit 562643d

Please sign in to comment.