Skip to content

Union type does not match conditional type  #23803

Closed
@kevinbeal

Description

@kevinbeal

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?

Activity

marcellerusu

marcellerusu commented on May 1, 2018

@marcellerusu

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

mhegazy commented on May 1, 2018

@mhegazy
Contributor

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

kevinbeal commented on May 1, 2018

@kevinbeal
Author

@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

kevinbeal commented on May 1, 2018

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

mhegazy commented on May 1, 2018

@mhegazy
Contributor

This example might be related to #23132.

mhegazy

mhegazy commented on May 1, 2018

@mhegazy
Contributor

Is another implementation planned?

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

mhegazy

mhegazy commented on May 1, 2018

@mhegazy
Contributor

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

added
Design LimitationConstraints of the existing architecture prevent this from being fixed
on May 1, 2018
typescript-bot

typescript-bot commented on May 16, 2018

@typescript-bot
Collaborator

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

locked and limited conversation to collaborators on Jul 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @kevinbeal@marcellerusu@mhegazy@typescript-bot

        Issue actions

          Union type does not match conditional type · Issue #23803 · microsoft/TypeScript