Skip to content

Typescript typings issue with Button component prop in combination with certain other props (like varient) #22452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 tasks done
deldrid1 opened this issue Sep 2, 2020 · 2 comments
Labels
docs Improvements or additions to the documentation typescript

Comments

@deldrid1
Copy link

deldrid1 commented Sep 2, 2020

I am trying to follow along with https://material-ui.com/components/buttons/#third-party-routing-library and leveraging the nextjs-with-typescript example from material-ui. It appears that in some cases TypeScript has issues with certain combinations of properties.

Current Behavior 😯

I've made two changes from https://github.com/mui-org/material-ui/tree/master/examples/nextjs-with-typescript to demonstrate the expected behavior and the bug.

  • In pages/about.tsx, on lines 17-19, I've replaced the Link component with a Button using the component={Link} prop as mentioned in the third-party-routing-library guide. I have also added a variant="outlined" prop to style the button, which causes TypeScript to throw this error:
No overload matches this call.
  Overload 1 of 3, '(props: { href: string; } & { children?: ReactNode; color?: "inherit" | "default" | "primary" | "secondary" | undefined; disabled?: boolean | undefined; disableElevation?: boolean | undefined; ... 6 more ...; variant?: "text" | ... 2 more ... | undefined; } & { ...; } & CommonProps<...> & Pick<...>): Element', gave the following error.
    Type '{ children: string; component: ForwardRefExoticComponent<Pick<LinkProps, "slot" | "style" | "title" | "className" | "classes" | "innerRef" | "key" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | ... 272 more ... | "prefetch"> & RefAttributes<...>>; href: string; variant: "outlined"; }' is not assignable to type 'IntrinsicAttributes & { href: string; } & { children?: ReactNode; color?: "inherit" | "default" | "primary" | "secondary" | undefined; disabled?: boolean | undefined; ... 7 more ...; variant?: "text" | ... 2 more ... | undefined; } & { ...; } & CommonProps<...> & Pick<...>'.
      Property 'component' does not exist on type 'IntrinsicAttributes & { href: string; } & { children?: ReactNode; color?: "inherit" | "default" | "primary" | "secondary" | undefined; disabled?: boolean | undefined; ... 7 more ...; variant?: "text" | ... 2 more ... | undefined; } & { ...; } & CommonProps<...> & Pick<...>'.
  Overload 2 of 3, '(props: { component: ForwardRefExoticComponent<Pick<LinkProps, "slot" | "style" | "title" | "className" | "classes" | "innerRef" | "key" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | ... 272 more ... | "prefetch"> & RefAttributes<...>>; } & { ...; } & { ...; } & CommonProps<...> & Pick<...>): Element', gave the following error.
    Type 'string' is not assignable to type 'undefined'.
  Overload 3 of 3, '(props: DefaultComponentProps<ExtendButtonBaseTypeMap<ButtonTypeMap<{}, "button">>>): Element', gave the following error.
    Type '{ children: string; component: ForwardRefExoticComponent<Pick<LinkProps, "slot" | "style" | "title" | "className" | "classes" | "innerRef" | "key" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | ... 272 more ... | "prefetch"> & RefAttributes<...>>; href: string; variant: "outlined"; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; color?: "inherit" | "default" | "primary" | "secondary" | undefined; disabled?: boolean | undefined; ... 7 more ...; variant?: "text" | ... 2 more ... | undefined; } & { ...; } & CommonProps<...> & Pick<...>'.
      Property 'component' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; color?: "inherit" | "default" | "primary" | "secondary" | undefined; disabled?: boolean | undefined; ... 7 more ...; variant?: "text" | ... 2 more ... | undefined; } & { ...; } & CommonProps<...> & Pick<...>'.ts(2769)
Button.d.ts(55, 5): The expected type comes from property 'variant' which is declared here on type 'IntrinsicAttributes & { component: ForwardRefExoticComponent<Pick<LinkProps, "slot" | "style" | "title" | "className" | "classes" | "innerRef" | "key" | "defaultChecked" | ... 274 more ... | "prefetch"> & RefAttributes<...>>; } & { ...; } & { ...; } & CommonProps<...> & Pick<...>'

Expected Behavior 🤔

  • In pages/index.tsx I've made the same change but instead of using variant="outlined", I've used color="secondary" and everything works as expected.

Steps to Reproduce 🕹

Refer to this codesandbox for a reproduction of the bug: https://codesandbox.io/s/bold-water-9xdld?file=/pages/about.tsx

Context 🔦

I'm guessing this is a silly TypeScript issue (I'm still learning and appreciate the value but also get frustrated by silly things like this). I want to be able to use the full power of buttons and not just certain combinations so this is blocking me. Other workarounds I've tried have various drawbacks as well..

Your Environment 🌎

Tech Version
Material-UI v4.11.0
React v16.13.1
TypeScript v4.0.2 (see codesandbox for .tsconfig)
Next.js v9.5.3
@deldrid1 deldrid1 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Sep 2, 2020
@eps1lon eps1lon added docs Improvements or additions to the documentation typescript status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer status: waiting for author Issue with insufficient information labels Sep 2, 2020
@eps1lon
Copy link
Member

eps1lon commented Sep 2, 2020

This was fixed in #21552 which is available in @material-ui/core@^5.0.0-alpha.1. We will not backport this fix to v4.

@codeater7

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to the documentation typescript
Projects
None yet
Development

No branches or pull requests

3 participants