Skip to content

Unexpected behavior of conditional types on generics #33296

Closed
@nscarcella

Description

@nscarcella

I stumbled upon a rather odd behavior while trying to combine conditional types with generics. It looks like, even when the produced types are structurally different, it's still possible to assign a values between them.

class T1 { t1_tag = 't1' }
class T2 extends T1 { t2_tag = 't2' }

type X<T> = {
  x: T extends T2 ? 'yes' : 'no'
}

let a1: X<T1> = { x: 'no' } // {x: 'yes'} is not valid
let a2: X<T2> = { x: 'yes' } // {x: 'no'} is not valid

// These assignations should not type, but they do
a1 = a2
a2 = a1

At first I thought this might be caused by generics being intentionally ignored but that seemed weird, since the types end up being structurally different and checks seem to work for literals, so I though I would report it, just in case.

TypeScript Version: 3.6.2

Search Terms: generics, conditional type, type inheritance

Expected behavior: exclusive structural conditional types derived from generic should not be assignable to each other.

Actual behavior: assignation seems to pass type check even when types are structurally incompatible.

Playground Link: here

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