TypeScript doesn't have a built-in non-empty array type, but you can define one using a tuple type: `type NotEmptyArray<T> = [T, ...T[]]`. This enforces at least one element at compile time. To prevent mutation-related issues (e.g., methods that could empty the array), a `ReadOnlyNotEmptyArray<T>` wraps it with `Readonly<>`. A type guard function `isNotEmptyArray` can narrow a regular array to this type at runtime by checking its length.

2m read timeFrom playfulprogramming.com
Post cover image
Table of contents
Post contentsWe couldn’t find what you were looking for.

Sort: