Skip to content

Error in exhaustive switch over generic argument limited to specific literal strings #986

Closed
@jkieboom

Description

@jkieboom

The following example is valid in the current compiler but not in the typescript based compiler:

interface A {
  type: "a";
}

interface B {
  type: "b";
}

interface Types {
  a: A;
  b: B;
}

export function exhaustiveSwitch<T extends keyof Types>(type: T): boolean {
  switch (type) {
    case "a":
      return true;
    case "b":
      return true;
  }
}

resulting in the error:

src/exhaustive-generic-switch.ts:14:67 - error TS2366: Function lacks ending return statement and return type does not include 'undefined'.

playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    Domain: Type CheckingRelated to type checking, grammar checking

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions