Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug: [no-unsafe-enum-comparison] should report when union is in switch and cases have union type #11596

@Afgan0r

Description

@Afgan0r

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play#ts=5.9.2&showAST=types&fileType=.tsx&code=KYOwrgtgBAIgggFTgfQQTQAoFEoG8BQUUAYgJYBOAzgC5QC8UADADSFQDKwAxgPYgAm9KAEZWAX3z5qATwAOwKAHFQwcgENqwfgjkKGBIjPkAuJlAA%2BIgNz4J%2BXiBpRywSrL6Vgp5SFUatOvJCBlBGXiLikpQA7qTUXAAWUAAULm4ewAB0YQCUeGxcap6wiCjo2JlkVNTGBUUK8EiomFiZnA78tUT8wABmamAANjW2kkA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6ZAd0vzIAt6AHrwCGsZPkoA3RExTJafRGQDW6MAG1w2CACYANFu1Zt2SMPjwA9hwAiiAGaj4%2BAMLDkiAGKXoAUSGi4lKIAMpcPLxq%2BHCIBiamZJZMyJQAJojQdo4I%2BP4iYhLS3tAAqkyUSagYjvAecfFQ0IgAjrCUTVlO%2BMUAckllFUxqNR6G2AC%2BYwC6WpPjQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

enum DATA_TYPE {
  First = 0,
  Second = 1,
}

type GeneratedType = {
  type: 0 | 1;
}

const response: GeneratedType = {
  type: 1,
}

switch (response.type) {
  case DATA_TYPE.First:
  case DATA_TYPE.Second:
  default:
}

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/switch-exhaustiveness-check": [
      2,
      {
        "allowDefaultCaseForExhaustiveSwitch": true,
        "considerDefaultExhaustiveForUnions": false,
        "requireDefaultForNonUnion": false
      }
    ]
  },
};

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

I expect that eslint do not throw any error in switch

Actual Result

ESLint throws error in switch:

Switch is not exhaustive. Cases not matched: 0 | 1

Additional Info

I use openapi-typescript to generate response types based on swagger. It generates the following type:

type BackendV1Components = {
     schemas: {
        PublicationType: 0 | 1;
        PublicationViewModel: {
            type: BackendV1Components["schemas"]["PublicationType"];
        };
    }
}

I also have an enum that represent these values:

export enum PUBLICATION_TYPE {
  Standard = 0,
  LessonResult = 1,
}

But when I try to use the enum when processing the response, ESLint gives me the error described above:

const process = (model: BackendV1Components['schemas']['PublicationViewModel']) => {
  // Switch is not exhaustive. Cases not matched: 0 | 1
  switch (model.type) {
    case PUBLICATION_TYPE.Standard:
    case PUBLICATION_TYPE.LessonResult:
    default: 
  }
}

In addition, TypeScript correctly checks whether the enum member is equal to the case value. If I change the LessonResult member to 2, then TypeScript will throw an error:

const process = (model: BackendV1Components['schemas']['PublicationViewModel']) => {
  // Switch is not exhaustive. Cases not matched: 0 | 1
  switch (model.type) {
    case PUBLICATION_TYPE.Standard:
    // Type 'PUBLICATION_TYPE.LessonResult' is not comparable to type '0 | 1'. ts(2678)
    case PUBLICATION_TYPE.LessonResult:
    default: 
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions