-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Refine unions in contextual typing #16457
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
+1 |
Microsoft has the current version at typescript.com |
seems to be the same underlying issue as #18365 |
I have a fix for the first example, but note that the first example won't magically work because function f(xy: X | Y) {
if (xy.value === 'done') {
// xy: X | Y
}
else {
// xy: X | Y
}
} That's because, as a discriminant, |
Another alternative here is to always maintain the literal type if the contextual type is its base type. i.e. give |
I think #19587 will fix this. |
Should this code work after fix? interface Control {
CurrentMode: "View" | "Edit";
}
let controls: Control[];
let controlsToEdit: Control[] = controls.map(c => c
? { CurrentMode: "Edit" }
: c); For now (ts 2.8.3) I get |
Source issue #16450 and others (TODO: find them)
This code should work, but doesn't:
Similar example:
We can, in principle, make this work by discarding
X
from the union.In the first example, this can be done (somehow) by using the discriminating union from the object literal.
In the second example, this can be done by noting that we have a fresh object literal type (meaning there are known to be no additional properties) and a missing
type1
property.The text was updated successfully, but these errors were encountered: