-
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
Union type does not match conditional type #23803
Comments
Afaik, ternary operator doesn't work on type level. It also doesn't make much sense what it'd actually do here imo. What's wrong with doing it like function test(arg: any[] | number): any[] | number {
return Array.isArray(arg) ? [] : 1;
} -- |
Similar to #22984. This is a design limitation of the current implementation, the relationship between the resulting types and the conditional type is lost through the ternary operation. the compiler can not verify the relationship at the moment. The alternative is to use overloads to model your example. function test(arg: any[]): any[];
function test(arg: number): number;
function test(arg: any[] | number): any[] | number {
return Array.isArray(arg) ? [] : 1;
} |
@MarcelRusu Conditional types (with ternary expressions) were added in 2.8 I have a situation where I have this interface:
I want variables implementing this interface to be aware of the |
@mhegazy Okay, you can close this then, but it doesn't work for my actual use case. I just offered a simplified example. Is another implementation planned? |
This example might be related to #23132. |
we continue to add new inferences, so it is possible. |
Depending on how you are using |
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: 2.9.0-dev.20180501
Search Terms:
union type does not match conditional type
Code
Expected behavior:
No errors and for the result of
test()
to be known as array or number.It's possible there is a good reason for this behavior, but it certainly seems like it should work, to me (with Typescript as my first type annotated language).
Actual behavior:
Error:
Type 'undefined[] | 1' is not assignable to type 'T extends any[] ? any[] : number'.
Type 'undefined[]' is not assignable to type 'T extends any[] ? any[] : number'.
Playground Link: https://www.typescriptlang.org/play/#src=function%20test%3CT%3E(arg%3A%20T)%3A%20T%20extends%20any%5B%5D%20%3F%20any%5B%5D%20%3A%20number%20%7B%0D%0A%20%20%20%20return%20Array.isArray(arg)%20%3F%20%5B%5D%20%3A%201%3B%0D%0A%7D
Related Issues:
Unknown?
The text was updated successfully, but these errors were encountered: