Skip to content

TypeScript silently omits enum assignment type error between two enums with the same name #55915

Closed
@evanw

Description

@evanw

🔎 Search Terms

I'm not exactly sure what keywords to use for this because it's kind of a strange problem. Some ideas: enum assignment same name error missing

🕗 Version & Regression Information

  • This is the behavior in every version I tried (both TypeScript 3.3 and TypeScript 5.2)

⏯ Playground Link

No response

💻 Code

I couldn't do a playground link for this because it requires two separate files:

  • types.ts

    export enum DiagnosticCategory {
        Warning,
        Error,
        Suggestion,
        Message,
    }
    
    export enum DiagnosticCategory2 {
        Warning,
        Error,
        Suggestion,
        Message,
    }
  • diagnosticInformationMap.generated.ts

    import { DiagnosticCategory as DiagnosticCategory2 } from "./types";
    // import { DiagnosticCategory2 as DiagnosticCategory2 } from "./types";
    
    enum DiagnosticCategory {
      Warning = 'Warning',
      Error = 'Error',
      Suggestion = 'Suggestion',
      Message = 'Message',
    }
    
    function diag2(category1: DiagnosticCategory, category2: DiagnosticCategory2) {
      category1 = category2;
      category2 = category1;
    }

🙁 Actual behavior

There is no error if you import DiagnosticCategory as DiagnosticCategory2 but there is an error if you DiagnosticCategory2 as DiagnosticCategory2. My tsconfig.json for this experiment:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "strict": true
  }
}

Demo:

$ node_modules/.bin/tsc -version
Version 5.2.2

$ head -n 2 diagnosticInformationMap.generated.ts 
import { DiagnosticCategory as DiagnosticCategory2 } from "./types";
// import { DiagnosticCategory2 as DiagnosticCategory2 } from "./types";

$ node_modules/.bin/tsc -p .
(no errors)

$ head -n 2 diagnosticInformationMap.generated.ts 
// import { DiagnosticCategory as DiagnosticCategory2 } from "./types";
import { DiagnosticCategory2 as DiagnosticCategory2 } from "./types";

$ node_modules/.bin/tsc -p .
diagnosticInformationMap.generated.ts:12:3 - error TS2322: Type 'DiagnosticCategory2' is not assignable to type 'DiagnosticCategory'.

12   category1 = category2;
     ~~~~~~~~~

diagnosticInformationMap.generated.ts:13:3 - error TS2322: Type 'DiagnosticCategory' is not assignable to type 'DiagnosticCategory2'.

13   category2 = category1;
     ~~~~~~~~~


Found 2 errors in the same file, starting at: diagnosticInformationMap.generated.ts:12

🙂 Expected behavior

I expected importing DiagnosticCategory as DiagnosticCategory2 to cause the same error that importing DiagnosticCategory2 as DiagnosticCategory2 does. My rationale is that the two type definitions in types.ts appear to be identical to me, so my intuition tells me to expect them to behave identically. It seems to me like TypeScript is perhaps silently swallowing the error about assignment compatibility if the two types are named the same thing even though the names refer to two separate incompatible things from two separate files.

Additional information about the issue

I discovered this confusing behavior while helping @jakebailey on #55894. It seemed like a bug to me so I'm reporting it here. Feel free to close this issue if this is expected behavior.

Metadata

Metadata

Labels

Breaking ChangeWould introduce errors in existing codeBugA bug in TypeScriptExperimentation NeededSomeone needs to try this out to see what happensFix 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