-
Notifications
You must be signed in to change notification settings - Fork 12.8k
No validation in JSX when using template string index signatures #44797
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
Comments
I think that since string index signatures actually do cover these properties, this is just an oversight in implementation and can be considered a bug. |
Okay, this is bananas. import * as React from "react";
interface PropsA {
foo: string;
[dataProp: string]: string;
}
function ComponentA(props: PropsA) {
return <div />;
}
// Error on data-yadda ✅
let x = <ComponentA foobar="hello" data-yadda={42} />;
/////////////////////
interface PropsB {
foo: string;
[dataProp: string]: string;
}
function ComponentB(props: PropsB) {
return <div />;
}
// No error ❌ 😲
let y = <ComponentB foo="hello" data-yadda={42} />; |
The bananas behavior is because we ignore JSX attributes with dashed names in relationship checking, but then fail to ignore them in error elaboration. That's a pretty simple fix. |
No, string index signatures actually don't cover dashed names today. They only do in the broken error elaboration logic because it fails to ignore the dashed names. It would definitely be a breaking change to always validate dashed names against string index signatures. Best non-breaking change we could consider is to not validate dashed names against string index signatures, but to validate them against template literal index signatures. But I'm not sure I could ever explain the rationale behind that with a straight face. |
We're going to have to do that with #44815 anyway. 😅 |
Fix for the strange error elaboration behavior in #44873. Still not sold on validation against template string index signatures. |
It's worth mentioning that we can always do the checking later and loosen it after. Maybe @dzearing can give some context on the use-case that Fluent UI had in mind. |
I've filed #46229 as an alternative that allows the break to be opt-in for all index signatures; however, I think there's a lot of value in just removing the exemption for all non |
It this the same bug I'm running into? I'm using dashes in index signatures for the tag name (IntrinsicElements), not in attributes. I think it might have the same underlying cause, so I'm bringing it up so that a solution takes this into account and not just attributes. |
Playground Link
Here's the shortest example:
Expected: TypeScript should error on
data-yadda
, just as if we had explicitly declared adata-yadda
property inProps
, or if we had used astring
index signature.Actual: JSX dashed properties are exempt from these checks on the target object if a corresponding property doesn't exist.
My expectation is that if an index signature exists, and a property should be covered by that index signature, then TypeScript should check that the property is compatible with that index signature.
Here's a longer example that could be used in tests.
The text was updated successfully, but these errors were encountered: