Skip to content

Casting enums with same values gets error #39127

@zlk89

Description

@zlk89

TypeScript Version: 3.9.2

Search Terms:

Expected behavior:
It would be great to check if all enum values are exclusively overlap, and only throw error when it is not.

Actual behavior:
Now it shows error even when they have exactly same values:

Conversion of type 'EnumA' to type 'EnumB' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.(2352)

Related Issues:

Code

export enum EnumA {
  Key = 'VALUE',
}

export enum EnumB {
  Key = 'VALUE',
}

function assign(val: EnumA) {
  const val2: EnumB = val as EnumB;  // <--- getting error here
  return val2;
}
Output
export var EnumA;
(function (EnumA) {
    EnumA["Key"] = "VALUE";
})(EnumA || (EnumA = {}));
export var EnumB;
(function (EnumB) {
    EnumB["Key"] = "VALUE";
})(EnumB || (EnumB = {}));
function assign(val) {
    const val2 = val;
    return val2;
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

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