utility-types

repository·master·Indexed 26 days ago

https://github.com/piotrwitek/utility-types

A 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+.

Tokens
10.9K
Snippets
56
Records
70
Agent score
88%

What's inside utility-types

  1. Perform set operations on union types

    master

    Perform various set operations on union types using the following utilities:

    • SetIntersection<A, B>: Returns the intersection of union types A and B (same as built-in Extract).
    • SetDifference<A, B>: Returns the difference of union types A and B (same as built-in Exclude).
    • SetComplement<A, A1>: Returns the complement of union type A relative to its subset A1.
    • SymmetricDifference<A, B>: Returns the difference between the union and the intersection of A and B.
  2. Deep utility types for nested structures

    master

    The library provides deep versions of common utility types to work with deeply nested structures:

    • DeepReadonly<T>: Makes all properties in a nested structure readonly.
    • 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.
  3. Filter union types by undefined or null

    master

    Use these types to remove specific nullish values from a union:

    • NonUndefined<A>: Excludes undefined from the union A.
    • NonNullable<A>: Excludes both undefined and null from the union A (Note: this is a built-in TypeScript type, but provided here for completeness).
  4. Use utility-types mapped types

    master

    The 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.
  5. Extract Promise and Value types

    master

    Utility types for extracting types from containers:

    • PromiseType<T>: Obtains the resolved type of a Promise<T>.
    • ValuesType<T>: Gets the union type of all values in an object, array, or array-like type T.
    // 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>;
  6. Extract object keys by property type

    master

    Retrieve union types of keys from an object T based 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> (or WritableKeys<T>): Keys that are not readonly.
    • ReadonlyKeys<T>: Keys that are readonly.
    • 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 type U.
    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 }>