A collection of useful TypeScript utility types to enhance your type system experience. These types cover various scenarios such as deep merging, value extraction, permutations, and more. You can easily import them for better type manipulation in your projects.
You can install the package via npm or yarn:
npm install @dhlx/types
# or
yarn add @dhlx/types
This package includes the following utility types:
Converts a union type into an intersection type.
type Result = UnionToIntersection<{ a: string } | { b: number }>;
// Result: { a: string } & { b: number }
Generates all possible combinations of characters from a string.
type Result = AllCombinations<'abc'>;
// Result: '' | 'a' | 'b' | 'c' | 'ab' | 'ac' | 'bc' | 'abc'
Combines two string types A
and B
into a single string type by concatenating them.
type Result = CrossJoin<'a', 'b'>;
// Result: 'ab'
Flattens a nested object type into a single level, with keys as dot-notation paths.
type Result = Flatten<{ a: { b: { c: number } } }>;
// Result: { 'a.b.c': number }
Deeply merges two types T
and U
, merging nested objects recursively.
type Result = DeepMerge<{ a: { b: number } }, { a: { c: string } }>;
// Result: { a: { b: number, c: string } }
Extracts the resolved value type of a Promise
, recursively resolving nested promises.
type Result = DeepPromiseValueType<Promise<Promise<number>>>;
// Result: number
Replaces all occurrences of a substring S
in a string T
with the string M
.
type Result = Replace<'hello world', 'world', 'everyone'>;
// Result: 'hello everyone'
Extracts unique values from a union type, removing duplicates.
type Result = Unique<[1, 2, 2, 3, 3, 4]>;
// Result: [1, 2, 3, 4]
Recursively picks properties from a nested object type, based on a dot-notation string path.
type Result = DeepPick<{ a: { b: { c: number } } }, 'a.b.c'>;
// Result: { c: number }
Makes all properties of a type T
and its nested properties readonly
.
type Result = DeepReadonly<{ a: { b: number } }>;
// Result: { readonly a: { readonly b: number } }
Filters the keys of an object T
where the corresponding value type extends U
.
type Result = FilterKeys<{ a: number, b: string, c: boolean }, string>;
// Result: 'b'
Omit the properties of an object T
that have a value type extending U
.
type Result = OmitByType<{ a: number, b: string, c: boolean }, string>;
// Result: { a: number, c: boolean }
Generates all possible permutations of a string T
.
type Result = Permutation<'abc'>;
// Result: 'abc' | 'acb' | 'bac' | 'bca' | 'cab' | 'cba'
Reverses a tuple or array type T
.
type Result = Reverse<[1, 2, 3]>;
// Result: [3, 2, 1]
Generates all subsequences of a string T
.
type Result = Subsequences<'abc'>;
// Result: '' | 'a' | 'b' | 'c' | 'ab' | 'ac' | 'bc' | 'abc'
Ensures that the values of a union type T
are unique.
type Result = Unique<[1, 'a', 2, 'b', 1]>;
// Result: [1, 'a', 2, 'b']
Extracts the properties of an object type T that are optional.
type Result = GetOptional<{ a: number, b?: string, c?: boolean }>;
// Result: 'b' | 'c'
Makes all properties of a type T and its nested properties optional recursively.
type Result = DeepPartial<{ a: { b: { c: number } } }>;
// Result: { a?: { b?: { c?: number } } }
import { UnionToIntersection, AllCombinations, DeepMerge } from 'typescript-utils';
type Result = UnionToIntersection<{ a: string } | { b: number }>;
// Result: { a: string } & { b: number }
type Combos = AllCombinations<'abc'>;
// Combos: '' | 'a' | 'b' | 'c' | 'ab' | 'ac' | 'bc' | 'abc'
type Merged = DeepMerge<{ a: { b: number } }, { a: { c: string } }>;
// Merged: { a: { b: number, c: string } }
MIT License. See LICENSE for more information.