Why Truthy Checks can Break on Zero in TypeScript
JavaScript/TypeScript can treat non-boolean values as booleans in certain situations. Basically, there are a set of values which are defined as Falsy. All other values are considered Truthy. When c...

Source: DEV Community
JavaScript/TypeScript can treat non-boolean values as booleans in certain situations. Basically, there are a set of values which are defined as Falsy. All other values are considered Truthy. When combined with Optional Properties, code can be more concise and easier to read. Consider a system for filtering numerical values where each filter property is optional. type FilterOptions = { greaterThan?: number, lessThan?: number, }; /* Returns true if `value` satisfies the filter, false otherwise */ function isInFilter(value: number, filter: FilterOptions): boolean { if (filter.greaterThan && value <= filter.greaterThan) return false; if (filter.lessThan && value >= filter.lessThan) return false; return true; } /* Demonstrates the filtering function by logging the numbers between -5 and 5 that satisfy the filter */ function filterExample(filter: FilterOptions) { const results:number[] = []; for (let i = -5; i <= 5; i++) if (isInFilter(i, filter)) results.push(i); co