Community TypeScript Snippet

The Five Custom TS Utility Types I Add to Every Project

TypeScript ships great utility types but the five I miss in every fresh project are these: NonEmptyArray, Branded, Awaited inverse (Promisify), DeepReadonly, and ExactKeys. Each is one or two lines that has saved me from a bug.

The Five Custom TS Utility Types I Add to Every Project

TypeScript ships great utility types but the five I miss in every fresh project are these: NonEmptyArray, Branded, Awaited inverse (Promisify), DeepReadonly, and ExactKeys. Each is one or two lines that has saved me from a bug.

TypeScript
Frontend
4 snippets
type-system
generics
code-template
tylerperry

By @tylerperry

March 21, 2026

·

Updated May 20, 2026

834 views

14

4.3 (14)

NonEmptyArray is the tiny win that prevents the most common TypeScript bug I see in code review: indexing an array and getting T | undefined. By declaring at the type level that the array has at least one element, downstream code can use arr[0] without a guard. Branded<T, B> solves the opposite-shape problem: the compiler treats string as a universal currency for ids, but in practice mixing up a UserId and an OrgId is exactly the kind of bug that ships to production. The phantom __brand field is erased at runtime, so the cost is zero, and the compiler treats UserId and OrgId as incompatible.