-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inconsistent property type deduction with type guard. #53453
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
Since making a type alias changes the behavior, I’m guessing this has something to do with variance measurement. |
Possibly a duplicate of #48936 (comment). |
Proof that variance measurement affects how type Xtrue = X<true>;
declare let a: X;
declare let b: X<true>;
declare let c: Xtrue;
b = a; // ok
c = a; // error! TS seems to think that |
This is ultimately a case of unmeasurable variance that is being measured anyway, which is a known design limitation. Specifically, the compiler measures the The example works when an alias is injected because we only rely on measured variance when relating instances of the same type alias. In general, 4.9 and 5.0 both exhibit the variance measurement inconsistency as illustrated by @fatcerberus here. The reason the example changed between 4.9 and 5.0 is because narrowing by user defined type predicates (such as |
Doesn’t that imply it should pick the asserted type then? |
Let me rephrase that... We stay with the original type if it is a subtype of the asserted type; otherwise, we narrow to the asserted type if it is a subtype of the original type; otherwise, if the types aren't related at all, we produce an intersection of the types. |
Thanks for the responses. I still want to ask the same question. If this is a known limitation, is there a known workaround (besides the working example)? |
Bug Report
I created this issue to reopen #53313. It was marked as
Not a defect
, but there are follow-up questions from me that are unanswered. I still believe both cases described below should have the same behavior.🔎 Search Terms
type guard, control flow analysis
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The Failed example failed at deducing its property, while in the Working example, only by aliasing
type A = X<true>
, the error is gone.🙂 Expected behavior
Both examples should have the same behavior.
The text was updated successfully, but these errors were encountered: