Skip to content

[3.7.1.-rc] Regression: exhaustive switch statements are not exhaustive anymore #34716

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

Closed
karol-majewski opened this issue Oct 24, 2019 · 2 comments

Comments

@karol-majewski
Copy link

TypeScript Version: 3.7.1.-rc

Search Terms: noFallthroughCasesInSwitch, exhaustive, exhaustiveness, switch

Code

interface ProcessEnv {
  NODE_ENV: 'development' | 'production';
}

declare const env: ProcessEnv;

declare function getEnvironmentVariable<K extends keyof ProcessEnv>(key: K): ProcessEnv[K];

const getNodeEnvironment = () => { // 💣 Not all code paths return a value.ts(7030)
  switch (getEnvironmentVariable('NODE_ENV')) {
    case 'development':
      return 'development';
    case 'production':
      return 'production';
  }
};

Expected behavior:

No error. The expression provided to switch evaluates to either 'development' or 'production', so the switch statement is exhaustive.

Interestingly enough, if we assign the expression to a local variable, the function becomes exhaustive.

const getNodeEnvironment = () => { // ✅ No error
  const environment = getEnvironmentVariable('NODE_ENV');

  switch (environment) {
    case 'development':
      return 'development';
    case 'production':
      return 'production';
  }
};

Actual behavior:

A compile-time error for getNodeEnvironment.

Not all code paths return a value.ts(7030)

Playground Link

@fatcerberus
Copy link

Duplicate of #34661, it looks like.

@karol-majewski
Copy link
Author

You're right, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants