Assume having the following code in typescript:
const x = [1];
if (x.length !== 1)
throw "error";
x.pop();
if (x.length === 0)
throw "error";
the compliler weirdly complains at line if (x.length === 0)
and says This comparison appears to be unintentional because the types '1' and '0' have no overlap.ts(2367)
It seems after the first condition, it always assumes the length is 1 and array modifications doesn’t change it. I don’t understand the reason and how can we fix it?
Playground link: https://www.typescriptlang.org/play/?ssl=9&ssc=15&pln=1&pc=1#code/MYewdgzgLgBAHjAvDA2gRgLoG4CwAofASwDMYAKOAOgBsBTMAcygAsYBCRZNASnxYCcQAdxgAiWv0H9RuAnioAHEArLdZRUhRr0mrTsgAMvPAOFiJUmUA