Skip to content

inconsistent infer behaviour #26815

@albert-mirzoyan

Description

@albert-mirzoyan

TypeScript Version: 3.1.0-dev.20180831

Search Terms: infer, unused generic type

In below example infer behaves inconsistently when the generic type argument is unused.

It should either:

  • infer {} for all cases due to some "unused hence ignore" logic
  • "extends" should not assert it
  • "expected behavior".

In real life scenario T is used to carry additional type info.

Code

type Unpack<A> = A extends Parent<infer T>? T : never;
interface Parent<T> {
    //foo:T
}
interface Child extends Parent<string>{
    bar
}
type Cousin = Parent<string> & {
    baz
}

type PP = Parent<string>;

type T1 = Unpack<Parent<number>>;   //T1 is number OK
type T2 = Unpack<Child>;            //T2 is {}     NOT-OK
type T3 = Unpack<PP>;               //T3 is string OK
type T4 = Unpack<Cousin>;           //T4 is {}     NOT-OK

Expected behavior:
T1 to infer number
T2 to infer string
T3 to infer string
T4 to infer string

Actual behavior:
T1 infers number
T2 infers {}
T3 infers string
T4 infers {}

Workaround:
Uncomment foo, use the generic type in any form

Playground Link:
Playground Link

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions