truthy
A value that counts as true in a condition such as if (value), which is every value except the falsy ones.
In more detail
if (!param) return removes null and undefined, but also 0 and the empty string, which may be valid. Narrowing by truthiness is convenient but check that no legitimate falsy value is thrown away.
Related terms
- falsy — A value that counts as false in a condition: false, 0, -0, 0n, the empty string, null, undefined and NaN.
- type narrowing — The compiler refining a broad type to a more specific one inside a block, after a check proves which case applies.
- control flow analysis — The compiler following every branch, return and throw in your code to work out the narrowest type a variable can have at each line.
Taught in
- Typeof Guards, Truthiness And Equality Narrowing — Production TypeScript
- The in Operator And Instanceof Narrowing — Production TypeScript
- Assertion Signatures: asserts condition And Invariants — Production TypeScript