Flow doesn't check type in switch when case is a variable
- Dominant language
- Rust
- Stars
- 22.3k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
Flow doesn't do any type checking on variable passed as a case in a switch operator.
🔴 __Flow reports no errors:__ expected to have errors on the 2nd and 3rd cases
```js
type Status = 'A' | 'B';
const unsupportedStatus = { c: 'C' };
const process = (status: Status): number => {
switch (status) {
case 'A': {
return 1;
}
case unsupportedStatus: {
return 2;
}
case unsupportedStatus.c: {
return 3;
}
default: {
return 0;
}
}
}
```
[Try it](https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgDKGAhhgK4DOYAvGAOQCC9YAPgwEL0DcqAxnAB2lDGHLDyOHHABOGPABNiZKrTABvMHwBcDAMIsAvr35CRYHDLh88lanQAUIlZV3KKlAJS7B5ALYARngytAB8GqhglAgAlhh8ABZgTqQenhFgmXwklARM9LrqkZmZMngUMoJgAIy8JYbFWjkE4pSS0nKK7lSFjaXl5JVgAEx1mQ0l2bliElKy8kqpVAB0OhklYGUVVQDMY2ATmQp4UCTkMBi9G5sDQwAM+4cNDUA)
✅ __Flow reports an error when directly using a string not in the union type__:
```js
type Status = 'A' | 'B';
const process = (status: Status): number => {
switch (status) {
case 'A': {
return 1;
}
case 'C': {
return 2;
}
default: {
return 0;
}
}
}
```
[Try it](https://flow.org/try/#0PQKgBAAgZgNg9gdzCYAoVAXAngBwKZgDKGAhhgK4DOYAvGAOQCC9YAPgwEL0Dc6AxnAB2lDGBwAnOHzyVqdABQiyVAFxFSFSgEo1g8gFsARnnG0AfGADeqMJQQBLDHwAWYRRqparNsGD4lKAiZ6NWtfcPE8CnFBMABGXnCAXx8-AKCAYRDvcN9I6NiAJkTfFPCAEzwoEnIYDFDUvKjyGLAABhKwMq7UFKA)
Contributor guide
Assessment
This issue has not been assessed yet.