Skip to content

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

Closed

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'.'
*/

Activity

  1. changed the title [-]Discriminated unions over enum members don't error as expected when substituted with number literals[/-] [+]Discriminated unions over enum members don't error as expected when used with number literals[/+] on Dec 4, 2016
  2. normalser commented on Dec 6, 2016

    @normalser

    I think it's rather about "feature" of enums - #11559

    const enum E { Hey, Ho }
    const a: E.Hey = 54 // // Doesn't error
  3. sushruth commented on Mar 24, 2019

    @sushruth

    I am seeing a related error in similar situation -

    const enum kinds {
        first,
        second,
    }
    
    interface IFData {
        kind: kinds.first;
        data: string;
    }
    
    interface ISData {
        kind: kinds.second;
        data: number;
    }
    
    type Data = IFData | ISData;
    
    function checker(input: Data) {
        const { kind, data } = input;
        switch (kind) {
            /**
             * Property 'length' does not exist on type 'string | number'.
             * Property 'length' does not exist on type 'number'.
             * ^ Above error on following case line
             */
            case kinds.first: return data.length;
    
            /**
             * Property 'length' does not exist on type 'string | number'.
             * Property 'length' does not exist on type 'number'.
             * ^ Above error on following case line
             */
            case kinds.second: return Math.abs(data);
        }
    }

    Discriminated unions with enum kinds do not seem to work as I expected. Can anyone suggest possible correction other than going back to literal string kinds ?

  4. RyanCavanaugh commented on Mar 26, 2019

    @RyanCavanaugh
    Member

    Sushruth Sastry (@sushruth) narrowing doesn't work once the discriminant is picked off into its own variable via destructuring

  5. sushruth commented on Mar 28, 2019

    @sushruth

    Yeah, I kind of figured that. It would have been awesome if it did work.

  6. added
    Working as IntendedThe behavior described is the intended behavior; this is not a bug
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Aug 22, 2019
  7. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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