-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[Button][material-ui] Button href TypeScript error #39287
Comments
assign this task to me |
@kmoyse-apizee I can't reproduce this: https://codesandbox.io/s/https-github-com-mui-material-ui-issues-39287-whkjn3 Could you provide a repro? You can fork my sandbox or this template: https://mui.com/r/issue-template-latest |
can you assign it to me ? |
@legendary0911 Are you able to reproduce the issue? |
Hi, Thanks for your answer and the reproduction. I forked it to reproduce my problem : https://codesandbox.io/s/https-github-com-mui-material-ui-issues-39287-forked-hc4nwk It seems to be more related to the 'href' property rather than the 'target' itself.
The error : Seems happens because my href constant coulld be undefined... I don't know why the issue shows on vs-code and not on codesandbox ? my tsconfig has the same configuration... |
@kmoyse-apizee The initial state in your snippet is undefined, you can fix it by initializing with an empty string: -const [href, setHref] = useState<string>();
+const [href, setHref] = useState<string>(''); |
Yes but in my application logic the variable in the href can actually be undefined. To fix the problem I had to do :
Which is fine because if not defined the button becomes disabled. I still don't get why same code works on codesandbox and cannot be compiled by typescript in my environment. |
@kmoyse-apizee - I do not think that this code will be maintainable in the long time run. I'd advise you to use to create a conditional wrapper for this component. I'm struggling to reproduce your issue locally and the sandbox is not working for me for some reason. |
While not a solution, and I think this should still be addressed, I have a workaround for you @kmoyse-apizee. If you conditionally render the property, it should avoid typescript issues. Below is an example from my own project I'm working on, where I'm creating my own Button component and rendering out MUI's button below:
As you can see, I'm also doing the same thing for the href, with some additional renderings for the component. I ran into the same issue you did and I was just looking for a way to avoid TS errors. |
Duplicates
Latest version
Steps to reproduce 🕹
Hi, I found issue that really looks like mine : #22699
Steps:
TS2769: No overload matches this call.
Overload 1 of 3, '(props: { href: string; } & ButtonOwnProps & Omit<ButtonBaseOwnProps, "classes"> & CommonProps & Omit<Omit<DetailedHTMLProps<AnchorHTMLAttributes, HTMLAnchorElement>, "ref"> & { ...; }, "color" | ... 24 more ... | "startIcon">): Element', gave the following error.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
Trying to set strictNullChecks: true in tsconfig.json does not solve the problem
Current behavior 😯
typescript transpile error
Expected behavior 🤔
No response
Context 🔦
No response
Your environment 🌎
System:
OS: Linux 5.19 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
Binaries:
Node: 16.18.0 - /usr/local/bin/node
Yarn: 1.22.19 - ~/.yarn/bin/yarn
npm: 9.6.4 - /usr/local/bin/npm
Browsers:
Chrome: 117.0.5938.132
npmPackages:
@emotion/react: ^11.11.1 => 11.11.1
@emotion/styled: ^11.11.0 => 11.11.0
@mui/base: 5.0.0-beta.17
@mui/core-downloads-tracker: 5.14.11
@mui/icons-material: ^5.14.11 => 5.14.11
@mui/material: ^5.14.11 => 5.14.11
@mui/private-theming: 5.14.11
@mui/styled-engine: 5.14.11
@mui/system: 5.14.11
@mui/types: 7.2.4
@mui/utils: 5.14.11
@types/react: ^18.2.12 => 18.2.12
react: ^18.2.0 => 18.2.0
react-dom: ^18.2.0 => 18.2.0
typescript: ^4.9.5 => 4.9.5
The text was updated successfully, but these errors were encountered: