I understand this may be a classical javascript issue, but I too often found myself using :
if (!something) {
//...
}
to validate that this something
is not undefined
or null
, in TypeScript.
This is very error-prone! When used on a number
"0" will match, and when used on an enum
the first item will match too (by default, the first item has a value of "0")!
Is there a way to deal with this in TypeScript? Is there a way to configure TypeScript to disallow an exclamation mark in front of anything except a boolean
(and any
)? Would this configuration make sense or am I missing something trivial?
Should :
if (something === null || something === undefined) {
//...
}
be used instead, to validate that something is defined or not? And is there a way to enforce this in a team?