TypeScript: narrowing types via type guards and assertion functions

1 ยท Axel Rauschmayer ยท June 7, 2020, midnight
In TypeScript, a value can have a type that is too general for some operations โ€“ for example, a union type. This blog answers the following questions: What is narrowing of types? (Spoiler: focusing on a subset of an overly general type, in a specific region of a program) What are type guards and assertion functions and how can we use them to narrow types? [Read rest of post]...