Skip to content

Report errors for duplicate case clauses #854

Closed
@DanielRosenwasser

Description

@DanielRosenwasser

It is incredibly easy to make a copy/paste error for case clauses in a switch statement.

switch (node.kind) {
    // ...
    case SyntaxKind.TupleType:
        // Do something with tuple types.
    case SyntaxKind.TupleType:
        // Do something with union types.
    // ...
}

While it is easily arguable that non-exhaustive cases can be allowed in TypeScript, there is no reason not to report duplicated cases.

"Obvious" semantics for case clauses in a single switch statement are as follows:

  • Report when named entities with identical symbols are handled.
  • Report when equal string literals are handled.
  • Report when equal numeric literals (even 0.0 and 0) are handled.
  • Report when null is handled more than once.
  • Report when undefined is handled more than once.

One interesting question is whether exhaustive cases for an enum should report an error when there is a default clause.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions