Is this a TypeScript bug or am I missing something? Variable type changes based on whether it's in an array method or not. This causes type issues and the only workaround I can think of is an additional typecast in a function call.
const fn = (arg: string) => {}
let variable: undefined | string
if(!variable) {
variable = 'asd'
}
fn(variable); // Variable type is string
[].map(() => {
fn(variable) // Variable type is string | undefined
})
I expect the variable type to change to string
once it's reassigned in an if
statement. This result is present in the function scope but the variable type falls back to string | undefined
in Array.prototype.map()
method.