TypeScript Utilities
-
Prettify
Takes an object type and makes the hover overlay more readable.
type Prettify<T> = { [K in keyof T]: T[K]; } & {}; -
Maybe
Returns a union of the type and a potentially missing value, such as undefined or null. It's using
NoInferto provide a more readable hover overlay.type Maybe<T> = NoInfer<T | undefined | null>; -
NonEmptyArray
Represents an array that has to have at least 1 element in it.
type NonEmptyArray<T> = [T, ...Array<T>]; -
UniqueElements
Returns a union of the unique elements of the array.
type UniqueElements<T extends unknown[]> = T[number]; -
DuplicateElements
Returns a union of the duplicate elements found in the array.
type DuplicateElements< T extends unknown[], Seen = never, Duplicates = never, > = T extends [infer First, ...infer Rest] ? First extends Seen ? DuplicateElements<Rest, Seen, Duplicates | First> : DuplicateElements<Rest, Seen | First, Duplicates> : Duplicates; -
UniqueArray
Represents an array that can contain only unique elements. Using branded types to provide a more readable hover overlay.
declare const brand: unique symbol; type ValidElements<T> = [T] & { [brand]: "ValidElements" }; type UniqueArray<T extends unknown[]> = DuplicateElements<T> extends never ? T : ValidElements<UniqueElements<T>>; -
AnyFn
Represents a function that can take any number and type of arguments and return any. Sometimes TypeScript forces you to use it in generics.
type AnyFn = (...args: any[]) => any;