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

Switch statements exhaustiveness check is failing when the expression has generic type but with literal type constraint #60658

Closed
jfet97 opened this issue Dec 2, 2024 · 0 comments Β· Fixed by #60644

Comments

@jfet97
Copy link
Contributor

jfet97 commented Dec 2, 2024

πŸ”Ž Search Terms

switch exhaustiveness literals

πŸ•— Version & Regression Information

This is mostly related with the new capabilities from #56941, so I'd say TS 5.8.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20241202#code/C4TwDgpgBACghiANgezgEygXgFBSgHygG8oB9YOAcwC4oAiOAV2AAs6BuKRgZwgCcAdnAC2EWt2B8AlgMqcwcbtwDuyPmnGSZlKAF9cBYmQo16AYzh9gHKFOARh3WgEE+fBAB4SUjVAnTZTgBHRjgBYDsQWgFGYQAjfj0APj0DQhJyKlo6SD5uZAEbIVFNALkoKjEoGPjE3XZsbFBIWAQUdABhAvtwrGIDAG0YWwFWpFQMRVgBukzKOgBdBdoAeWE7DxgAGno5uiSG+saAM0YBMwiCqGFkMwBreHH0TdIAFWcAcSgIAA8etG4Y3aaBmewWSQAFAZ-LQYG9PltsABKWFtCZdcIQcJDeEfBb9PAqOxmFhQCH+JEEvBQCy8ehMVh0agGalQPgQYCMQRGHj8YpVOj2CR0HYKJSqdTZdDrQp6Bqs2nQOgWKxMlnU9mc7neeyOWgDby+OgARgATABmEVQEJhCKgWhm816fFHBWKJW5fKFZmsvCarmjEj8qUCNDsuBWyq0U0Adjl6oA9AmoGgIMcmIhgD7fUmoKw+MhlNUIEWAKJuNQQuhnO4CQujEx0JHy1K6IA

πŸ’» Code

type Payload =
  | { _tag: "auth"; username: string; password: string }
  | { _tag: "cart"; items: Array<{ id: string; quantity: number }> }
  | { _tag: "person"; name: string; age: number };

type PayloadContent = {
  [P in Payload as P["_tag"]]: Omit<P, "_tag">;
};

function mockPayload<P_TAG extends Payload["_tag"]>(
  str: P_TAG,
): PayloadContent[P_TAG] {
  switch (str) {
    case "auth":
      return { username: "test", password: "admin" };
    case "cart":
      return { items: [{ id: "123", quantity: 123 }] };
    case "person":
      return { name: "andrea", age: 27 };
    // default:
      // throw new Error("unknown tag");
  }
}

πŸ™ Actual behavior

Error: Function lacks ending return statement and return type does not include 'undefined'.

πŸ™‚ Expected behavior

The exhaustiveness check should understand that there is no missing case to handle, so the default case should be optional.

Additional information about the issue

It's the companion issue for PR #60644 that will fix this if merged.

@jfet97 jfet97 changed the title sSwitch statements exhaustiveness check is failing when the expression has generic type but with literal type constraint Switch statements exhaustiveness check is failing when the expression has generic type but with literal type constraint Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant