utility-types
repository·master·Indexed 26 days ago
https://github.com/piotrwitek/utility-typesA collection of idiomatic, zero-runtime-cost TypeScript utility types that complement built-in mapped types and aliases. It provides tools for object transformations (DeepPartial, DeepReadonly), key/value selection (PickByValue, OmitByValue), set operations on union types, and Flow-inspired utilities like $Keys and $Values. Version 3.11.0 requires TypeScript v3.1+.
What's inside utility-types
- If you need assistance or want to discuss the project, the primary community support channel is the Spectrum chat.
Follow project updates
masterTo stay informed about new releases and updates forutility-types, you can follow the maintainer on Twitter or support the project via BuyMeACoffee.Install utility-types via npm or yarn
masterInstall the
utility-typespackage to access a collection of idiomatic TypeScript utility types that complement built-in mapped types.# NPM npm install utility-types # YARN yarn add utility-typesExclude null and undefined from types
masterUse these utilities to remove specific values from a union type:
NonNullable<A>: Excludesnullandundefinedfrom setA.NonUndefined<A>: Excludesundefinedfrom setA.
Perform set operations on union types
masterPerform various set operations on union types using the following utilities:
SetIntersection<A, B>: Returns the intersection of union typesAandB(same as built-inExtract).SetDifference<A, B>: Returns the difference of union typesAandB(same as built-inExclude).SetComplement<A, A1>: Returns the complement of union typeArelative to its subsetA1.SymmetricDifference<A, B>: Returns the difference between the union and the intersection ofAandB.
Deep utility types for nested structures
masterThe library provides deep versions of common utility types to work with deeply nested structures:
DeepReadonly<T>: Makes all properties in a nested structurereadonly.DeepRequired<T>: Makes all properties in a nested structure required.DeepNonNullable<T>: Makes all properties in a nested structure non-nullable.DeepPartial<T>: Makes all properties in a nested structure optional.
Check utility-types TypeScript compatibility
masterEnsure your TypeScript version meets the requirements for the version of
utility-typesyou are using:v3.x.x- TypeScript v3.1+v2.x.x- TypeScript v2.8.1+v1.x.x- TypeScript v2.7.2+
Filter union types by undefined or null
masterUse these types to remove specific nullish values from a union:
NonUndefined<A>: Excludesundefinedfrom the unionA.NonNullable<A>: Excludes bothundefinedandnullfrom the unionA(Note: this is a built-in TypeScript type, but provided here for completeness).
Use utility-types internal symbols
masterThe library exports several internal-use symbols (prefixed with
$) for advanced type manipulation:$Call$Diff$ElementType$Keys$NonMaybeType$PropertyType$ReadOnly$Shape$ValuesClass
Use utility-types mapped types
masterThe library provides a wide range of mapped types for manipulating object structures, sets, and unions. Key categories include:
- Object Transformations:
DeepPartial,DeepReadonly,DeepRequired,Mutable,Writable,Overwrite,Assign,Intersection. - Key/Value Selection:
Omit,OmitByValue,OmitByValueExact,PickByValue,PickByValueExact,OptionalKeys,RequiredKeys,ReadonlyKeys,FunctionKeys,NonFunctionKeys,MutableKeys,WritableKeys. - Set Operations:
SetComplement,SetDifference,SetIntersection,Subtract,SymmetricDifference,Unionize,UnionToIntersection. - Other Utilities:
Brand,NonUndefined,PromiseType,ValuesType.
- Object Transformations:
Extract Promise and Value types
masterUtility types for extracting types from containers:
PromiseType<T>: Obtains the resolved type of aPromise<T>.ValuesType<T>: Gets the union type of all values in an object, array, or array-like typeT.
// PromiseType // Expect: string; type Response = PromiseType<Promise<string>>; // ValuesType type Props = { name: string; age: number; visible: boolean }; // Expect: string | number | boolean type PropsValues = ValuesType<Props>; type NumberArray = number[]; // Expect: number type NumberItems = ValuesType<NumberArray>; type NumberTuple = [1, 2]; // Expect: 1 | 2 type NumberUnion = ValuesType<NumberTuple>;Extract object keys by property type
masterRetrieve union types of keys from an object
Tbased on the property's characteristics:FunctionKeys<T>: Keys where the value is a function.NonFunctionKeys<T>: Keys where the value is not a function.MutableKeys<T>(orWritableKeys<T>): Keys that are notreadonly.ReadonlyKeys<T>: Keys that arereadonly.RequiredKeys<T>: Keys that are required (not optional).OptionalKeys<T>: Keys that are optional.UnionKeys<U>: The union of all keys present in any object within the union typeU.
type MixedProps = {name: string; setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any;}; // Expect: "setName | someFn" type Keys = FunctionKeys<MixedProps>; // Expect: "name | someKey" type Keys = NonFunctionKeys<MixedProps>; // Expect: "bar" type Props = { readonly foo: string; bar: number }; type Keys = MutableKeys<Props>; // Expect: "foo" type Props = { readonly foo: string; bar: number }; type Keys = ReadonlyKeys<Props>; // Expect: "req" | "reqUndef" type Props = { req: number; reqUndef: number | undefined; opt?: string; optUndef?: number | undefined; }; type Keys = RequiredKeys<Props>; // Expect: "opt" | "optUndef" type Props = { req: number; reqUndef: number | undefined; opt?: string; optUndef?: number | undefined; }; type Keys = OptionalKeys<Props>; // Expect: 'name' | 'age' | 'visible' UnionKeys<{ name: string; age: string } | { age: number } | { visible: boolean }>