#TypeScript type narrowing: Use a truthy condition to remove null/undefined...


#TypeScript type narrowing:

Use a truthy condition to remove null/undefined

Use "typeof" to narrow a union of primitives

Use "instanceof" to narrow class types

Use "in" to narrow object types

Use a function type guard with "is" to check the value and establish its type.
A short post with examples of each of these approaches: https://www.carlrippon.com/6-ways-to-narrow-types-in-typescript/

View original on X