-
Notifications
You must be signed in to change notification settings - Fork 12.8k
TypeScript ignores type of JSX spread props if the JSX tag is non-empty #44782
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
Duplicate of #29883 The one that has an error has an error for a separate reason; the real behavior you’re talking about is the fact that excess properties are generally allowed on JSX elements if they come from spread. The former example just happens to error because it has no good properties (whereas the latter has a matching |
#29883 is framed in an odd way, but just a few comments down it’s argued and broadly 👍’d that it feels like a bug. Either way, this is behavior that we do know about and at one point did on purpose I guess, so changing it is “suggestion,” not a “bug fix” 🤷 |
The bug aspect could be fixed independently by allowing excess properties in general. Technically this can't break any existing code. From a user perspective it would be clearer to allow excess properties entirely or not at all. That the language treats Of course the cleaner / desired solution is to disallow them (via an option). |
That’s not an excess-property thing; that’s a common-property thing, and the example you found just looks extra strange because of the way JSX obscures what’s going on under the hood. You can suppress the error not only by making the element non-empty, but by adding any other recognized prop: <div {...badProps}></div> // error: no properties in common
<div {...badProps} className="whatever"> </div> // no error, because source and target have overlap with `children`
<div {...badProps} className="whatever"></div> // no error, because source and target have overlap with `className` The “no properties in common logic” exists to catch likely mistakes where there would otherwise be no error: playground. There’s no type-theoretical reason to treat these two objects differently, since excess properties are allowed and all the target properties are optional. But when there’s zero overlap, it looks quite likely to be a mistake, so we flag it. So yes, you can often find strange examples that look highly inconsistent with each other when you put them side-by-side, but in this case that inconsistency is an unrelated feature, not a bug. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
jsx empty
Possibly related issues:
Related SO question: Why does TypeScript ignore the type of JSX spread props if the JSX tag is not empty?
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
This assumes JSX: React
🙁 Actual behavior
The expression in
Test2
compiles without error.🙂 Expected behavior
Type checks should apply to both empty and non-empty usages, and result in the same compilation error.
The text was updated successfully, but these errors were encountered: