Skip to content

Discriminated unions behave inconsistently since v4.6Β #49475

Closed
@azhiv

Description

@azhiv

Bug Report

πŸ”Ž Search Terms

Discriminated union

πŸ•— Version & Regression Information

  • This changed between versions 4.5.5 and 4.6.4

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

class Model {
  constructor(public flag: boolean) {}
}

type DiscriminatedUnion = { flag: true } | { flag: false };
class A<T extends DiscriminatedUnion> {
  constructor(public model: T) { }
}

// Uncomment the two lines below to get rid of the compiler error:
// function foo(_model: DiscriminatedUnion) { }
// foo({} as Model);

class B extends A<Model> { }    // Type 'Model' is not assignable to type '{ flag: false; }'.

πŸ™ Actual behavior

Class A doesn't accept Model as generic parameter with the following error:

Type 'Model' is not assignable to type '{ flag: false; }'.
  Types of property 'flag' are incompatible.
    Type 'boolean' is not assignable to type 'false'.

The basic example above hadn't emitted any errors prior to v4.6. Furthermore, if you uncomment the code that declares and executes function foo, the compiler error vanishes.

πŸ™‚ Expected behavior

The code is compilable in 4.6+ without any workarounds.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions