Skip to content

The behavior is different for two code that looks the same #30988

Description

@kgtkr

TypeScript Version: 3.4.2

Search Terms:
conditional type

Code

OK

type F<X> = <T>() => T extends X ? 1 : 2;

declare const x: F<any>;
const y: F<number> = x;

Error

declare const x: <T>() => T extends any ? 1 : 2;
const y: <T>() => T extends number ? 1 : 2 = x;

Expected behavior:
The two codes do the same thing

Actual behavior:
Different behavior

Playground Link:

Related Issues:

Activity

  1. jack-williams commented on Apr 17, 2019

    @jack-williams
    Collaborator

    I think the effect of the type alias F is causing reduction in the body of the alias, in particular, I think it's simplifying the return type to the constraint of the conditional 1 | 2. Once this happens, the type parameter X no longer appears in the type and the two signatures are related.

    type F<X> = <T>() => T extends X ? 1 : 2;
    declare const x: F<number>;
    const y: F<string> = x; // no error
    
    type F2<X> = <T>() => T extends X ? 1 : 2;
    const y2: F2<string> = x; // error

    Haven't got as far as tracking down where exactly this behaviour comes from, but I'm guessing there is some instantiation business going on.

  2. kgtkr commented on Apr 18, 2019

    @kgtkr
    Author

    Related code

    image

  3. added
    UnactionableThere isn't something we can do with this issue
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Aug 22, 2019
  4. RyanCavanaugh commented on Aug 22, 2019

    @RyanCavanaugh
    Member

    These samples are quite different and it's not apparent why they must behave identically.

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

    UnactionableThere isn't something we can do with this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions