-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisHelp WantedYou can do thisYou can do this
Milestone
Description
π Search Terms
switch default discriminated Unions
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about discriminated unions
β― Playground Link
π» Code
enum AnimalType {
cat = 'cat',
dog = 'dog'
}
type Animal = {
type: `${AnimalType.cat}`;
meow: string;
} | {
type: `${AnimalType.dog}`;
bark: string;
}
function check(p: never) {
throw new Error('Error!')
}
function action(animal: Animal) {
// Compiles
if (animal.type === AnimalType.cat) {
console.log(animal.meow);
} else if (animal.type === AnimalType.dog) {
console.log(animal.bark);
} else {
check(animal)
}
// Compiles
switch (animal.type) {
case `${AnimalType.cat}`:
console.log(animal.meow);
break;
case `${AnimalType.dog}`:
console.log(animal.bark);
break;
default:
check(animal);
}
// Does not compile
switch (animal.type) {
case AnimalType.cat:
console.log(animal.meow);
break;
case AnimalType.dog:
console.log(animal.bark);
break;
default:
check(animal); // <-- Argument of type 'Animal' is not assignable to parameter of type 'never'
}
}π Actual behavior
The last example does not compile
π Expected behavior
To my understanding all three examples do exactly the same and all should compile.
Additional information about the issue
No response
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Control FlowThe issue relates to control flow analysisThe issue relates to control flow analysisHelp WantedYou can do thisYou can do this