-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
JSX prop with dash is not type checked #32447
Comments
This is the intended behavior because of |
@RyanCavanaugh Got it. Does it make sense to have an option to turn off this special case for |
If people start running into a lot, we could think about it, but it hasn't seemed to have been an issue so far |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
It would be great if we could allow only |
@RyanCavanaugh I support @agentcooper and @Pomar81 there, it looks like unexpected behavior 💭 |
I see at least three possible interpretations here and I'm not sure which people are going for:
Is this actually coming up as a problem? Standard props are never named with dashes so it seems like a very difficult mistake to make. |
from my side, it would be great if TS Team could apply last option |
This came up as a problem for me, when converting HTML to JSX (specifically MJML to mjml-react), and forgetting to convert some of the dashed prop names to camel case. There were no type errors, and essentially no validation on the props that were copied across. I was open to solving this with an ESLint rule, but there appears to be no ESLint rule either. |
Just hit this. We have a custom I would have expected it not to check the prop on the |
@RyanCavanaugh Time to revisit this issue, since template string types appeared? My use case is that I want to translate all properties like props : { [ key in 'class' | 'style' | `style:${ string }` ]? : string } So the following declaration is accepted fine (note the typo):
|
This also seems to accept |
Note for other people searching for this: Right now, it's actually possible to create type-checked Definition: interface Attributes {
[key: `class:${string}`]: boolean;
} Usage: <div class:whatever={true}>
<div class:whatever={false}>
<div class:whatever> <!-- evaluates to true --> |
I believe we just hit this on MDN too: a user reported no Due to this behaviour in TypeScript, we got absolutely no indication of the prop naming mismatch, which is surely the point of typing our component props. Could this bug be reopened? |
as well as data- and any other dashed props typescript won't alert us to a dashed prop being passed to a component where it isn't accepted, so it's safest to pass them through wholesale: microsoft/TypeScript#32447 partial fix to: #8015
pass through aria- props on Button component as well as data- and any other dashed props typescript won't alert us to a dashed prop being passed to a component where it isn't accepted, so it's safest to pass them through wholesale: microsoft/TypeScript#32447 partial fix to: #8015 * fix: improve aria labels on mobile sidebar button fixes: #8015 * dashedProps -> passthroughAttrs
Hi, are we have news about it? This is creating some problems for me. |
I'd say it could be useful to type restrict or control which hyphen-cased props are allowed, because sometimes in the component's internal logic, we are not forwarding A similar scenario occurred in MUI DataGrid when we a user tried to pass A similar use-case is also mentioned by @Peeja above. |
I think this is the biggest reason that this bug should be fixed. There is no guarantee that a component is spreading these props, so why always allow them? If a component did want to allow these props, they could always manually extend |
Another reason: Let's say you are spreading
You'd want to have the component's props be something like |
I am working on a library with a JSX API, in which hyphenated prop names are explicitly not allowed. Given the context of the library, however, my users may inadvertently write them anyway. Not only will the behavior they expect not occur if they do this, but in fact, in the case of my library, behavior that is actually erroneous will occur if they do this. As such, it's imperative that I enforce types that prevent them from doing this. But apparently, this is impossible. No matter how strictly I author my types, the type checker will always allow my users to specify props that I know are wrong. I am frustrated to learn that this is another case in which the type checker deliberately defeats its own safety. To be honest, from reading the documentation on typing JSX (which lead me to discover this issue), most of the type-checking behavior for JSX feels unsound. Whereas most of TypeScript's general type-checking behavior feels sound, the JSX behavior just feels like a series of special cases (such as this) stuck together. As it happens, the JSX API is actually optional in my library. It seems to me that I should warn my users against using it, since it turns out that JSX is actually not type safe. Perhaps I shouldn't even support it at all. |
One of our projects tried this to explicitly define
|
TypeScript Version: 3.5.1
Search Terms: jsx, dash, props
Code
Expected behavior:
Expected error on
el3
.Actual behavior:
No error on
el3
.Playground Link
The text was updated successfully, but these errors were encountered: