3 ms·
What happens in the following case? const foo: Maybe<boolean> = false; It seems to me that your code wouldn't behave they way you'd expect.
by adwn 3y ago
What happens in the following case?
const foo: Maybe<boolean> = false;
It seems to me that your code wouldn't behave they way you'd expect.
- ElFitz 3y agoThat’s because of the "truthy-ish" nonsense mess inherited from JavaScript. You could say the same with 0, "", NaN. That being said, I wonder what was the logic behind the decision to implement that.
- rezonant 3y agoCoercing nullishness/undefined-ness to false arises from languages like C. It is an unfortunate footgun, but not what most people think of when they think of Javascript's coercion weirdnesses. Typescript supports this because it is a strict superset of Javascript. They certainly could have made TS strict mode complain about it though.
- strken 3y agoIf you want to cover this particularly case, strict-boolean-expressions in tslint should catch it. That being said, I agree it's an unfortunate footgun and think tsc should yell at you for using non-boolean types in conditionals if strict is on.