Typescript - Incorrectly inferring 'never'
If you are absolutely sure that a
has a value there, than you can put the !
after the variable
let a: number | null = null;
[1].forEach(() => {
a = 1;
});
if (a !== null)
a!.toFixed(); //
I would not use null
thought but undefined
, so no need to use !
let a: number | undefined;
[1].forEach(() => {
a = 1;
});
if (a) {
a.toFixed(); // No problem here
}
Also as recommendation use !==
not !=