Skip to content
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

Closed
kevinbeal opened this issue May 1, 2018 · 8 comments
Closed

Union type does not match conditional type #23803

kevinbeal opened this issue May 1, 2018 · 8 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@kevinbeal
Copy link

TypeScript Version: 2.9.0-dev.20180501

Search Terms:
union type does not match conditional type

Code

function test<T>(arg: T): T extends any[] ? any[] : number {
    return Array.isArray(arg) ? [] : 1;
}

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?

@marcellerusu
Copy link

marcellerusu commented May 1, 2018

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;
}

--
EDIT: ignore me, didn't realize type level ternary exists

@mhegazy
Copy link
Contributor

mhegazy commented May 1, 2018

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;
}

@kevinbeal
Copy link
Author

kevinbeal commented May 1, 2018

@MarcelRusu

Conditional types (with ternary expressions) were added in 2.8

I have a situation where I have this interface:

interface ControlResult<T> {
	fields: ConvertPrimitiveTo<T, FormControl>;
	controls: T extends any[] ? FormArray : FormGroup;
}

I want variables implementing this interface to be aware of the controls field is specifically being FormGroup or FormArray.

@kevinbeal
Copy link
Author

@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?

@mhegazy
Copy link
Contributor

mhegazy commented May 1, 2018

This example might be related to #23132.

@mhegazy
Copy link
Contributor

mhegazy commented May 1, 2018

Is another implementation planned?

we continue to add new inferences, so it is possible.

@mhegazy
Copy link
Contributor

mhegazy commented May 1, 2018

Depending on how you are using ControlResult, an alternative in your other example is to lift the union type over your whole type.

@mhegazy mhegazy added the Design Limitation Constraints of the existing architecture prevent this from being fixed label May 1, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

4 participants