Skip to content

Discriminated unions over enum members don't error as expected when used with number literals #12647

Closed
@rotemdan

Description

@rotemdan

TypeScript Version: nightly (2.2.0-dev.20161203)

Code:

const enum E { Hey, Ho }

type A = {
	x: E.Hey;
	y: string;
}

type B = {
	x: E.Ho;
	y: number;
}
var zzz: A | B = { x: 1, y: "hello" }; // Doesn't error
var zzz: A | B = { x: 54, y: "hello" }; // Doesn't error

Expected behavior:
Should error.

Actual behavior:
Doesn't error.

For comparison:

type A = {
	x: 0;
	y: string;
}

type B = {
	x: 1;
	y: number;
}
var zzz: A | B = { x: 1, y: "hello" }; // Errors as expected
/*
'Type '{ x: 1; y: string; }' is not assignable to type 'A | B'.
  Type '{ x: 1; y: string; }' is not assignable to type 'B'.
    Types of property 'y' are incompatible.
      Type 'string' is not assignable to type 'number'.'
*/

var zzz: A | B = { x: 54, y: "hello" }; // Errors as expected
/*
'Type '{ x: 54; y: string; }' is not assignable to type 'A | B'.
  Type '{ x: 54; y: string; }' is not assignable to type 'B'.
    Types of property 'x' are incompatible.
      Type '54' is not assignable to type '1'.'
*/

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