Skip to content

identical conditional types not assignable to each other #21756

Closed

Description

@tvald

Identical conditional types are not assignable to each other.

TypeScript Version: 2.8.0-dev.20180208

Code

type Foo<T> = T extends string ? boolean : number;
type Bar<T> = T extends string ? boolean : number;
const convert = <T>(value: Foo<T>): Bar<T> => value; // this fails

type Baz<T> = Foo<T>;
const convert2 = <T>(value: Foo<T>): Baz<T> => value; // this passes

Expected behavior:
no errors

Actual behavior:

TypeScript $ ./node_modules/.bin/tsc --noEmit test.ts
test.ts(4,47): error TS2322: Type 'Foo<T>' is not assignable to type 'Bar<T>'.
  Type 'number | boolean' is not assignable to type 'Bar<T>'.
    Type 'number' is not assignable to type 'Bar<T>'.

Activity

  1. HerringtonDarkholme commented on Feb 8, 2018

    @HerringtonDarkholme
    Contributor

    IMHO, if a conditional type is already resolved, compare the resolved type.

    If target type is an unresolved conditional type, compare the source type:

    1. if source type is not unresolved conditional type, it can be assigned to the target if the type can be both assigned to true type and false type,
    2. if source type is unresolved conditional type, it can be assigned to the target if its branch types can be correspondingly assigned to target's branch types.

    But this usage sounds weird, Tony Valderrama (@tvald) could you please give a concrete example where you need to assign/compare two conditional types?

  2. tvald commented on Feb 8, 2018

    @tvald
    Author

    This is the particular case:

    // lib/es5.d.ts
    declare type PromiseConstructorLike = new <T>(executor: (
      resolve: T extends void ? (value?: PromiseLike<void>) => void : (value: T | PromiseLike<T>) => void,
      reject: (reason?: any) => void) => void
    ) => PromiseLike<T>;
    
    // lib/es2015.promise.d.ts
    interface PromiseConstructor {
      new <T>(executor: (
        resolve: T extends void ? (value?: PromiseLike<void>) => void : (value: T | PromiseLike<T>) => void,
        reject: (reason?: any) => void) => void
      ): Promise<T>;
    }

    It's reasonable to expect interoperability across compatible type definitions - perhaps originating from different libraries, perhaps narrowed from more complex types. Not supporting structural comparison of conditional types feels like a significant deviation from the way the rest of the type system works.

  3. added
    BugA bug in TypeScript
    and removed
    Needs More InfoThe issue still hasn't been fully clarified
    on Feb 8, 2018
  4. mhegazy commented on Feb 8, 2018

    @mhegazy
    Contributor

    This is behaving as intended, but i think we should reconsider.

  5. ahejlsberg commented on Feb 8, 2018

    @ahejlsberg
    Member

    I agree, two conditional types should be identical if they have the same four constituent types and the same distributivity (i.e. both are distributive or both are not). Pretty easy to fix, we already have logic in place to intern conditional types, it just needs a bit of tweaking.

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

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions