Types
Types: TypeScript utility types.
| Export | What it does |
|---|---|
AnyFunction | A type for any function, for generic constraints such as F extends AnyFunction (safer than Function, which also accepts classes and has an untyped call). |
Awaitable | A value that is returned directly or through a promise: the result of a callback that may be sync or async. |
Brand | A nominal type: a T that cannot be mixed up with another T of a different brand. |
Constructor | A type for any class producing a T: mixins, dependency injection tokens, instanceof helpers. |
DeepPartial | Makes every property optional, at every depth: a patch of a nested settings object, read-only since a patch is only read. |
DeepReadonly | Makes every property and array read-only, at every depth: a frozen configuration, a state snapshot that must not be changed in place. |
ElementOf | Reads the item type of an array or tuple, such as the type of a const list of options. |
Entries | The [key, value] pairs of an object type, each key with its own value type: the precise type of Object.entries, which widens keys to string. |
FirstParameter | Reads the type of the first parameter of a function, such as the event of a handler. |
KeysOfType | Lists the keys of the properties whose type matches: the numeric fields of a record to chart, the boolean flags of a settings object. |
LiteralUnion | A union of known literals that still accepts any value of the base type, without losing autocompletion of the literals (a plain 'a' | 'b' | string collapses to string). |
Merge | Combines two object types, the properties of the second replacing those of the first (like an object spread { ...a, ...b }), where an intersection would give never for conflicting properties. |
Mutable | Removes readonly from the properties of a type, one level deep: a builder filling an object before handing it out as read-only. |
NonEmptyArray | An array with at least one item, so that its first item is never undefined. |
Nullable | A T that may be null. |
Nullish | A T that may be null or undefined. |
Optional | A T that may be undefined. |
PartialKeys | Makes some properties optional: the input of a factory that fills them with defaults. |
PickByType | Keeps the properties whose type matches. |
RequireKeys | Makes some optional properties required: the result of filling in defaults. |
Simplify | Flattens intersections and mapped types into a plain object type, so that editor tooltips show the properties instead of A & Omit<B, 'c'>. |
UnionToIntersection | Turns a union into the intersection of its members: A | B gives A & B, to merge the types of a list of mixins or handlers. |
ValueOf | The union of the value types of an object type, such as a const object used as an enum. |