typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

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

Open jkieboom opened this issue 6 months ago • 0 comments

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

jkieboom avatar May 30 '25 09:05 jkieboom