-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Report errors if union subtype reduction results in fewer types than were explicitly written #3862
Comments
We should probably do #1809 first so that this code has somewhere reasonable to migrate to? |
The fix here would encapsulate more types of errors than just conflating Objects and primitives so I wouldn't say it's entirely necessary. In an ideal world if this is the majority of cases we see then yeah it might make sense to wait. On the other hand this would be a breaking change so the longer we wait the more impactful the break becomes. |
That gives people who want this pattern somewhere to go, but it doesn't get them there. If we implement #1809 tomorrow all the places where anyone wrote In addition, any case where one union constituent completely captures the set of values meant to be specified by another union constituent are still relevant. A contrived example: function foo(x: Base | Derived) { ... }
foo(new Derived2()); What was the developer's intention in saying |
I think that's clear in some contexts, but unclear in others. In the compiler we have some functions like (made up example): function isExportLike(node: CallExpression|Declaration) { ... } Then later I made Similarly, I wanted to write a function: function isDefineCall(node: Expression|CallExpression) {
// Pretend we have tagged types for a moment
if(node.kind === SyntaxKind.CallExpression) {
// Maybe return true
}
return false;
} This is probably an abuse of union types, but conveys well the intent of the function ("You can pass me anything, but I'm only going to possibly return true for |
Moot now that we don't immediately reduce union types. |
Based on examples seen in DefinitelyTyped. Someone misunderstood what
Object
means, thinking it meant non-primitive types (not an uncommon source of confusion), so they union it with the one primitive type they do want to allow. The compiler performs subtype reduction and ends up with onlyObject
. This is now allowing code the user intended to disallow. At this point if the compiler knows it has reduced an explicitly written union type to a set of types smaller than what was originally written it should just issue an error to alert the user that some part of their type annotation is not having the effect they expect.The text was updated successfully, but these errors were encountered: