You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeScript Version: 3.0.0-dev.201xxxxx
I was unable to try this. This is attempted on current playground version (2.8 or 2.9, the playground does not specify)
Search Terms: NonNullable, union
Code
declarefunctiontest<T>(condition: T): NonNullable<T>;enumA{ONE,TWO,}functionthing(x: A.ONE){// or a union type of some of the enum values.if(x===A.TWO){// x cannot be A.TWO}consty=test(x);if(y===A.TWO){// y can be A.TWO....}}
Expected behavior:
y should not be assignable to A.TWO. y should remain A.ONE type.
Actual behavior:
y becomes type A instead of incoming A.ONE type.
Literal types are not inferred unless there is a "marker" for the compiler e.g. constraint that suggests literal types are allowed.. so the type of y is A and not A.One.
you can get the desired behavior by defining test as:
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 3.0.0-dev.201xxxxx
I was unable to try this. This is attempted on current playground version (2.8 or 2.9, the playground does not specify)
Search Terms: NonNullable, union
Code
Expected behavior:
y should not be assignable to A.TWO. y should remain A.ONE type.
Actual behavior:
y becomes type A instead of incoming A.ONE type.
Playground Link:
https://www.typescriptlang.org/play/#src=declare%20function%20test%3CT%3E(condition%3A%20T)%3A%20NonNullable%3CT%3E%3B%0D%0A%0D%0Aenum%20A%20%7B%0D%0A%20%20ONE%2C%0D%0A%20%20TWO%2C%0D%0A%7D%0D%0A%0D%0Afunction%20thing(x%3A%20A.ONE)%20%7B%0D%0A%20%20if%20(x%20%3D%3D%3D%20A.TWO)%20%7B%0D%0A%20%20%20%20%2F%2F%20x%20cannot%20be%20A.TWO%0D%0A%20%20%7D%0D%0A%20%20const%20y%20%3D%20test(x)%3B%0D%0A%20%20%0D%0A%20%20if%20(y%20%3D%3D%3D%20A.TWO)%20%7B%0D%0A%20%20%20%20%2F%2F%20y%20can%20be%20A.TWO....%0D%0A%20%20%7D%0D%0A%7D%0D%0A
Related Issues: #23884 #23046
Thanks for taking a look!
The text was updated successfully, but these errors were encountered: