Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
Labels
accepting prsGo ahead, send a pull request that resolves this issuebugSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin
@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 havesearched for related issues and found none that matched my issue.
  • I haveread 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

enumDATA_TYPE{First=0,Second=1,}typeGeneratedType={type:0|1;}constresponse:GeneratedType={type:1,}switch(response.type){caseDATA_TYPE.First:caseDATA_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 useopenapi-typescript to generate response types based on swagger. It generates the following type:

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

I also have an enum that represent these values:

exportenumPUBLICATION_TYPE{Standard=0,LessonResult=1,}

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

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

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

constprocess=(model:BackendV1Components['schemas']['PublicationViewModel'])=>{// Switch is not exhaustive. Cases not matched: 0 | 1switch(model.type){casePUBLICATION_TYPE.Standard:// Type 'PUBLICATION_TYPE.LessonResult' is not comparable to type '0 | 1'. ts(2678)casePUBLICATION_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


      [8]ページ先頭

      ©2009-2025 Movatter.jp