TypeScript: JSX prop with dash is not type checked
TypeScript Version: 3.5.1
Search Terms: jsx, dash, props
Code
declare namespace JSX {
interface ElementAttributesProperty {
props: any;
}
}
class MyComponent {
props: {
foo?: string;
}
}
// OK
const el1 = <MyComponent foo="bar" />;
// OK, fooBar is incorrect prop
const el2 = <MyComponent fooBar="bar" />;
// Not OK, foo-bar is incorrect prop, but not reported
const el3 = <MyComponent foo-bar="bar" />;
Expected behavior:
Expected error on el3.
Actual behavior:
No error on el3.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 23
- Comments: 19 (6 by maintainers)
Commits related to this issue
- fix: pass through aira- 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 ... — committed to mdn/yari by LeoMcA a year ago
- fix(Button): pass through ARIA attrs (#8115) 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 ... — committed to mdn/yari by LeoMcA a year ago
It would be great if we could allow only
data-andaria-properties for JSX IntrinsicElements but throw an error in the other cases.If people start running into a lot, we could think about it, but it hasn’t seemed to have been an issue so far
@RyanCavanaugh I support @agentcooper and @Pomar81 there, it looks like unexpected behavior 💭
I believe we just hit this on MDN too: a user reported no
aria-labelon one of our buttons which had been set on our component, but on thearia-labelprop, whereas this component required anariaLabelprop.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?
@RyanCavanaugh Time to revisit this issue, since template string types appeared?
My use case is that I want to translate all properties like
style:${ name }to their corresponding reactive applicators. And now such properties are not typechecked even ifpropsis defined as:So the following declaration is accepted fine (note the typo):
from my side, it would be great if TS Team could apply last option
This also seems to accept
propName-as a valid case. We had a typopropName-={}and because of this it didn’t catch it. I think it should consider nothing after-as an invalid case. I guess that check shouldn’t break any aria related regexHi, are we have news about it? This is creating some problems for me.
This issue has been marked ‘Working as Intended’ and has seen no recent activity. It has been automatically closed for house-keeping purposes.
Another reason: Let’s say you are spreading
{ ...rest }to a component, but overriding a prop, i.e.You’d want to have the component’s props be something like
Omit<HtmlProps<HtmlDivElement>, 'aria-label'>, to disallowaria-labelfrom being passed, since it is overwritten. However, this currently does not work, asaria-labelis always allowed to be passed.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
HtmlPropsorAriaAttributesto their component’s props. But they should only be allowed if these are extended.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
{ ...rest }to the DOM element, and only forward a subset of props, which gives an incorrect notion that a certain prop is supported (or passed down to the dom element) which is not necessarily the use-case.A similar scenario occurred in MUI DataGrid when we a user tried to pass
data-*attributes which were actually not forwarded but the TS didn’t complain it.A similar use-case is also mentioned by @Peeja above.
Note for other people searching for this:
Right now, it’s actually possible to create type-checked
prefix:*attributes.Definition:
Usage:
Just hit this. We have a custom
Buttoncomponent. We forward a couple ofdata-attributes to the underlyingbutton. We just tried to add anotherdata-attribute to aButton, expecting it to pass through, but we didn’t realize we hadn’t forwarded that one yet. It type checked fine, because it was adata-attribute.I would have expected it not to check the prop on the
button, but to check it on theButton.This is the intended behavior because of
data-andaria-properties. If we ever get regex-based property names, we’ll revisit.