Closed as not planned
Description
π Search Terms
type narrow
π Version & Regression Information
since forever
β― Playground Link
π» Code
const list = ['A', 'B', 'C'] as const;
function abc(char: 'A' | 'B' | 'C' | 'D') {
if (list.includes(char)) { // errors here
return;
}
if (char == 'B') { } // should error here
// do something with char
}
π Actual behavior
TS complains about assignability
Argument of type 'string' is not assignable to parameter of type '"A" | "B" | "C"'.ts(2345)
π Expected behavior
for TS to recognize that I'm not trying to assign types in the wrong places
and instead to detect that the following comparison appears to be unintentional if (value == 'B') { }
function abc(char: 'A' | 'B' | 'C' | 'D') {
if (char == 'A' || char == 'B' || char == 'C') {
return;
}
if (char == 'B') { }
// do something with char
}
Additional information about the issue
I'm trying to add an early exit at the start of the function
and I want the type system to reflect that char
will no longer be 'A', 'B', 'C'
but instead it just tries to insult me