Skip to content

A type unified by conditional types loses some types #29945

Closed

Description

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:

Code

export type ElChildren =
  | ElChildren.Void
  | ElChildren.Text;
export namespace ElChildren {
  export type Void = undefined;
  export type Text = string;
}

type Relax<C extends ElChildren> = C extends ElChildren.Text ? ElChildren.Text : C;

export class Elem<
  C extends ElChildren,
  > {
  constructor(
    private children_: Relax<C>,
  ) {
  }
}

new Elem(undefined as ElChildren.Void);
new Elem('' as ElChildren.Text);
new Elem('' as ElChildren.Void | ElChildren.Text); // error
new Elem('' as ElChildren); // error

Expected behavior:

pass

Actual behavior:

Argument of type 'ElChildren' is not assignable to parameter of type 'undefined'.
  Type 'string' is not assignable to type 'undefined'.

Playground Link: http://www.typescriptlang.org/play/index.html#src=export%20type%20ElChildren%20%3D%0D%0A%20%20%7C%20ElChildren.Void%0D%0A%20%20%7C%20ElChildren.Text%3B%0D%0Aexport%20namespace%20ElChildren%20%7B%0D%0A%20%20export%20type%20Void%20%3D%20undefined%3B%0D%0A%20%20export%20type%20Text%20%3D%20string%3B%0D%0A%7D%0D%0A%0D%0Atype%20Relax%3CC%20extends%20ElChildren%3E%20%3D%20C%20extends%20ElChildren.Text%20%3F%20ElChildren.Text%20%3A%20C%3B%0D%0A%0D%0Aexport%20class%20Elem%3C%0D%0A%20%20C%20extends%20ElChildren%2C%0D%0A%20%20%3E%20%7B%0D%0A%20%20constructor(%0D%0A%20%20%20%20private%20children_%3A%20Relax%3CC%3E%2C%0D%0A%20%20)%20%7B%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Anew%20Elem(undefined%20as%20ElChildren.Void)%3B%0D%0Anew%20Elem(''%20as%20ElChildren.Text)%3B%0D%0Anew%20Elem(''%20as%20ElChildren.Void%20%7C%20ElChildren.Text)%3B%0D%0Anew%20Elem(''%20as%20ElChildren)%3B

Related Issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions