Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule suggestion: switch-exhaustiveness-check #1160

Open
lionel-rowe opened this issue May 29, 2023 · 2 comments
Open

Rule suggestion: switch-exhaustiveness-check #1160

lionel-rowe opened this issue May 29, 2023 · 2 comments
Labels
design limitation Currently this is a limitation of the design

Comments

@lionel-rowe
Copy link

lionel-rowe commented May 29, 2023

Disallow non-exhaustive switch statements. Especially useful when expanding an enum/string union/numeric union type to allow new options.

See https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/switch-exhaustiveness-check.md

Wrong ❌

type X = 'A' | 'B'

declare const x: X

switch (x) {
    case 'A':
        break
}

OK ✅

switch (x) {
    case 'A':
        break
    case 'B':
        break
}

OK ✅

switch (x) {
    case 'A':
        break
    default:
        break
}
@magurotuna
Copy link
Member

I would love this lint rule, but unfortunately this would require access to type information that TypeScript provides but deno_lint cannot access.

@magurotuna magurotuna added the design limitation Currently this is a limitation of the design label May 29, 2023
@lionel-rowe
Copy link
Author

lionel-rowe commented Jul 13, 2023

For anyone else that stumbles on this issue, I figured out a workaround via adding a dummy default clause. The non-exhaustive switch statement fails type checking with Type 'string' is not assignable to type 'never'. Playground

type X = 'A' | 'B'
declare const x: X

// `Type 'string' is not assignable to type 'never'.`
switch (x) {
    case 'A':
        break
    default: void ((): never => x)
}

// OK
switch (x) {
    case 'A':
        break
    case 'B':
        break
    default: void ((): never => x)
}

// OK
switch (x) {
    case 'A':
        break
    default:
        break
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design limitation Currently this is a limitation of the design
Projects
None yet
Development

No branches or pull requests

2 participants