-
Notifications
You must be signed in to change notification settings - Fork 12.8k
[5.0.2] Incorrect property type deduction with type guard #53313
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
There was a comment before about true false branch but it got deleted :(. Anyway, after that comment, I figure the following works too function doStuff(o: X<true> | X<false>): number {
if (hasDefinedField(o)) {
return o.x;
}
return 0;
} But I think |
I've deleted the comment because I have no clue why it's been different in 4.9. And |
Yes, type If<Condition extends boolean, TrueType, FalseType = undefined> = Condition extends true
? TrueType
: Condition extends false
? FalseType
: TrueType | FalseType;
type X<T extends boolean = boolean> = { x: If<T, number>; }
declare let a: X<boolean>;
declare let b: X<true> | X<false>;
a = b; This works, because |
For all |
So can you help we with a work around for this? Previously, in 4.9.5, I use type guard to force the type of an |
Sorry, but I think we have some minor miscommunication. What I want to point out is not to have a type guard to narrow down the type A = X<true>;
function hasDefinedField(o: X): o is A {
return o.x !== undefined;
}
function doStuff(o: X): number {
if (hasDefinedField(o)) {
return o.x;
}
return 0;
} This is also a working example. What I want here is to narrow |
Hi @RyanCavanaugh. Sorry for the mention, but I see you labeled this issue |
This issue has been marked as 'Not a Defect' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
type guard, control flow analysis
As this only breaks in version 5.0.2, I only found #53311 related to this issue but it seems like it is not the same.
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
The playground contains a working example and a failed example. The following is the failed one to keep it short.
🙁 Actual behavior
doStuff
,o.x
has the type ofnumber | undefined
.🙂 Expected behavior
o.x
should be number (aso
should beX<true>
). The working example shows this should be possible.The text was updated successfully, but these errors were encountered: