Skip to content

Unexpected type argument inferenceΒ #46623

Closed
@ltblue

Description

@ltblue

Bug Report

πŸ”Ž Search Terms

generic type argument inference

πŸ•— Version & Regression Information

4.4.4

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Type<T extends 1|2> = 
  T extends 1 ? number : 
  T extends 2 ? string :
  never;

type Foo<T extends 1|2> = (x: {a: T, b: Type<T>}) => void;

function foo1(x: {a: 1, b: number} | {a: 2, b: string}) {}
function foo2(x: {a: 2, b: string} | {a: 1, b: number}) {}

function bar<T extends 1|2> (a: T, b: Foo<T>) {}

bar(1, foo1); // ok
bar(2, foo1); // error: Argument of type '2' is not assignable to parameter of type '1'.(2345)

bar(1, foo2); // error: Argument of type '1' is not assignable to parameter of type '2'.(2345)
bar(2, foo2); // ok

πŸ™ Actual behavior

In bar, foo1 is always inferred to be Foo<1> and foo2 is always inferred to be Foo<2>

πŸ™‚ Expected behavior

It is supposed to be no error there because foo1 and foo2 have the same type, which can satisfy all the four calls.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions