-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Closed
Labels
accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issuebugSomething isn't workingSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugin
Description
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
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
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
Labels
accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issuebugSomething isn't workingSomething isn't workingpackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugin
{ "compilerOptions": { "strictNullChecks": true } }