Closed
Description
let someVar = null as null | number
// other logic here
if ([0, 1].includes(someVar)) {
// do something
}
Expected behavior:
No problem is returned.
Actual behavior:
TS returns the following problem on someVar
in the if statement:
Argument of type 'number | null' is not assignable to parameter of type 'number'.
Type 'null' is not assignable to type 'number'. ts(2345)
Meanwhile, changing the conditional from [0, 1].includes(someVar)
to ([0, 1] as (null|number)[]).includes(someVar)
fixes the problem. Why does Typescript implicitly type the array in this instance? The array isn't assigned so won't be used anywhere else, I don't see the utility in requiring this extra syntax here.
It would be nice to be able to use Array.includes()
as shorthand for a bunch of ||
operators such as in someVar === 0 || someVar === 1
.
Playground Link:
Related Issues:
Could not find related issues
Metadata
Metadata
Assignees
Labels
No labels