Skip to content

[typescript] Relax default-case rule #6906

@NeoLegends

Description

@NeoLegends

react-scripts v3.0.0


Hey!

We're using the new ESLint-linting for TypeScript. Works great so far, but a TS feature makes a lint obsolete:

const fn = (input: 'foo' | 'bar'): string => {
  switch (input) {
    case 'foo':
      return 'a';
    case 'bar':
      return 'b';
    // `input` can't have any other value than `foo` or `bar`
    // (so this switch is guaranteed to be exhausive), yet ESLint
    // complains with `default-case`
  }
}

In this case the switch is safe because TS guarantees exhausiveness, yet ESLint complains about a missing default-case. However, in this case it's actually detrimental to add a default case, because you're actively using the compiler's exhaustiveness-analysis for correctness. And you won't be warned anymore when extending the range of valid parameter values and forgetting the appropriate switch case.

As far as I can see it, there are three options:

  1. Disable the lint for TS projects
  2. Hook into tsc to validate the switch's exhausiveness and conditionally apply the lint
  3. Do nothing and leave the lint as is

I consider options 1 and 2 to be valid, and I'd prefer not to go with 3, because it removes some of TS type system's power.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions