dict
convert
OptionalToUndefined
type OptionalToUndefined<T>Convert optional properties to required ones that can be undefined
type R1 = OptionalToUndefined<{ a: 1, b?: 2 }>; // { a: 1, b: 2 | undefined }OptionalToUndefinedDeep
type OptionalToUndefinedDeep<T>Deeply convert optional properties to required ones that can be undefined.
type R1 = OptionalToUndefinedDeep<{ a: 1, b?: { c?: 4 } }>; // { a: 1, b: { c: 4 | undefined } | undefined }extract
ExtractByValue
type ExtractByValue<T, M extends Partial<Dict<Keys<T>>>>Extracts union members whose specified properties contain at least one of the specified value types.
type Obj = { a: string } | { a: number | boolean } | { a: null };
type R1 = Extract<Obj, { a: string | number }>; // { a: string }
type R2 = ExtractByValue<Obj, { a: string | number }>; // { a: string } | { a: number | boolean }ExtractByExactValue
type ExtractByExactValue<T, M extends Partial<Dict<Keys<T>>>>Extracts union members whose specified properties exactly match the specified value types.
type Obj = { a: string } | { a: string | boolean } | { a: null };
type R1 = Extract<Obj, { a: string | boolean }>; // { a: string } | { a: string | boolean }
type R2 = ExtractByExactValue<Obj, { a: string | boolean }>; // { a: string | boolean }ExtractFromValues
type ExtractFromValues<T, U, K extends SuggestKey<Keys<Known<T>>> = never>Extracts types assignable to U from values of T.
By default, extraction is applied to all properties. If K is specified, extraction is limited to those keys, leaving the remaining properties unchanged.
type T = { a: string | number, b: string | number };
type R1 = ExtractFromValues<T, string>; // { a: string, b: string };ExcludeFromValues
type ExcludeFromValues<T, U, K extends SuggestKey<Keys<Known<T>>> = never>Excludes types assignable to U from values of T.
By default, exclusion is applied to all properties. If K is specified, exclusion is limited to those keys, leaving the remaining properties unchanged.
type T = { a: string | undefined, b: string | undefined };
type R1 = ExcludeFromValues<T, string>; // { a: string, b: string };get
Get
type Get<T, K extends SuggestKey<Keys<Known<T>>>>An alias for Obj['Key'].
type R1 = Get<{ a: 1, b: 2 }, 'a'>; // 1Set
type Set<T, K extends SuggestKey<Keys<Known<T>>>, V>Set a value in an object.
type R1 = Set<{ readonly c?: 1 }, 'c', 4>; // { readonly c?: 4 }
type R2 = Set<{ a: 1 }, 'b', 2>; // { a: 1, b: 2 }If the key doesn't exist, it will be added.
GetDeep
type GetDeep<
T,
K extends SuggestKey<KeysDeep<Known<T>>, true>,
O extends boolean = false,
>Get a deeply nested value using dot-notation.
type Obj = { a: { b?: { c: 1 } } };
type R1 = GetDeep<Obj, 'a.b.c'>; // 1
type R2 = GetDeep<Obj, 'a.b.c', true>; // 1 | undefinedSet O to true to preserve undefined if one of the parents is optional.
SetDeep
type SetDeep<T, K extends SuggestKey<KeysDeep<Known<T>>, true>, V>Set a deeply nested value using dot-notation.
type R1 = SetDeep<{ a: { b: { c: 1 } } }, 'a.b.c', 4>; // { a: { b: { c: 4 } } }
type R2 = SetDeep<{}, 'a.b.c', 4>; // { a: { b: { c: 4 } } }
type R3 = SetDeep<{ a: boolean }, 'a.b.c', 4>; // { a: boolean }- If a parent key doesn't exist, it will be created as an empty object.
- If a parent key is not an object type, it won't be modified.
keyof
Keys
type Keys<T>Returns all possible keys.
type Config = { foo: string, bar: number } | { c: 4 };
type R1 = keyof Config; // never
type R2 = Keys<Config>; // 'foo' | 'bar' | 'c'Values
type Values<T>Returns all possible values.
type Config = { foo: string } | { c: 4 };
type R1 = Values<Config>; // string | 4Entries
type Entries<T>Returns all possible entries.
type Config = { foo: string, bar: number } | { c: 4 };
type R1 = Entries<Config>; // ["foo" | "bar", string | number] | ["c", 4]KeysDeep
type KeysDeep<
T,
R extends "leaf" | "parent" | "both" = "both",
_K extends string = "",
>A deep version of Keys.
type Config = { foo: string, bar: { baz: number } };
type R1 = KeysDeep<Config> // "foo" | "bar" | "bar.baz"
type R2 = KeysDeep<Config, "leaf"> // "foo" | "bar.baz"
type R3 = KeysDeep<Config, "parent"> // "bar"R controls whether to return leaf keys, parent keys, or both (default).
KeysOfType
type KeysOfType<T, U>Returns the keys of T whose values are assignable to U.
type R1 = KeyofType<{ a: 1, b: "b", c: 4 }, number>; // "a" | "c"
type R2 = KeyofType<{ a: 1, [x: string]: number }, number>; // string | numberKeysWithType
type KeysWithType<T, U>Returns the keys of T whose values have one of the types in U.
type Obj = { a: number, b: boolean | number };
type R1 = KeysOfType<Obj, number>; // "a"
type R2 = KeysWithType<Obj, number>; // "a" | "b"KeysWithExactType
type KeysWithExactType<T, U>KeysOfTypeDeep
type KeysOfTypeDeep<T, U, _K extends string = "">A deep version of KeysOfType.
type Config = { foo: string, bar: { baz: number } };
type R1 = KeysOfTypeDeep<Config, string> // "foo"
type R2 = KeysOfTypeDeep<Config, number> // "bar.baz"KeysWithTypeDeep
type KeysWithTypeDeep<T, U, _K extends string = "">KeysWithExactTypeDeep
type KeysWithExactTypeDeep<T, U, _K extends string = "">merge
MergeUnion
type MergeUnion<A, B, P extends boolean = false>Merge two objects, keeping all keys from both. On conflict, B wins.
type A = { readonly a: 1, b: 2 };
type B = { a: "<>", c: 4 };
type AB1 = MergeUnion<A, B>; // { a: "<>", b: 2, c: 4 }
type AB2 = MergeUnion<A, B, true>; // { readonly a: "<>", b: 2, c: 4 }Set P to true to use A's modifiers.
MergeUnionDeep
type MergeUnionDeep<A, B, P extends boolean = false>Merge two objects deeply, keeping all keys from both. On conflict, values are recursively merged if both are objects, otherwise B wins.
type A = { readonly a: 1, b: { c: 1, d: "b" } };
type B = { a: 99, b: { c: 4 } };
type AB1 = MergeUnion<A, B>; // { a: 99, b: { c: 4 } }
type AB2 = MergeUnionDeep<A, B>; // { a: 99, b: { c: 4, d: "b" } }- Set
Pto true to useA's modifiers. - Non-object members of
Aare discarded while those ofBare preserved.
MergePatch
type MergePatch<A, B, P extends boolean = false>Merge two objects, keeping all keys from A. On conflict, B wins.
type A = { readonly a: 1, b: 2 };
type B = { a: "<>", extra: "ignored" };
type AB1 = MergePatch<A, B>; // { a: "<>", b: 2 }
type AB2 = MergePatch<A, B, true>; // { readonly a: "<>", b: 2 }Set P to true to use A's modifiers.
MergePatchDeep
type MergePatchDeep<A, B, P extends boolean = false>Merge two objects deeply, keeping all keys from A. On conflict, values are recursively merged if both are objects, otherwise B wins.
type A = { a: 1, b: { c: 1, d: "b" } };
type B = { a: 99, extra: "key", b: { c: 4, extra: "key" } };
type AB1 = MergePatch<A, B>; // { a: 99, b: { c: 4, extra: "key" } }
type AB2 = MergePatchDeep<A, B>; // { a: 99, b: { c: 4, d: "b" } }- Set
Pto true to useA's modifiers. - Non-object members of
Aare discarded while those ofBare preserved.
MergeInt
type MergeInt<A, B, P extends boolean = false>Merge two objects, keeping all keys from A that exist in B. Values are taken from B.
type A = { readonly a: 1, b: 2 };
type B = { a: "<>", c: 4 };
type AB1 = MergeInt<A, B>; // { a: "<>" }
type AB2 = MergeInt<A, B, true>; // { readonly a: "<>" }Set P to true to use A's modifiers.
MergeIntDeep
type MergeIntDeep<A, B, P extends boolean = false>Merge two objects deeply, keeping all keys from A that exist in B. Values are recursively merged if both are objects, otherwise they are taken from B.
type A = { a: 1, b: { c: 1, d: "b" } };
type B = { a: 9, b: { c: 4 } };
type AB1 = MergeInt<A, B>; // { a: 9, b: { c: 4 } }
type AB2 = MergeIntDeep<A, B>; // { a: 9, b: { c: 4, d: "b" } }- Set
Pto true to useA's modifiers. - Non-object members of
Aare discarded while those ofBare preserved.
MergeDiff
type MergeDiff<A, B>Merge two objects, keeping all keys that exist only in A.
type A = { readonly a: 1, b: 2 };
type B = { a: "<>", c: 4 };
type AB = MergeDiff<A, B>; // { b: 2 }
type BA = MergeDiff<B, A>; // { c: 4 }MergeDiffDeep
type MergeDiffDeep<A, B>Merge two objects deeply, keeping all keys that exist only in A and recursively merging keys that exist in both when both values are objects.
type A = { a: 1, b: { c: 1, d: "b" } };
type B = { b: { d: "b" } };
type AB1 = MergeDiff<A, B>; // { a: 1 }
type AB2 = MergeDiffDeep<A, B>; // { a: 1, b: { c: 1 } }MergeSymDiff
type MergeSymDiff<A, B>Merge two objects, keeping all non-shared keys.
type A = { readonly a: 1, b: 2 };
type B = { a: "<>", c: 4 };
type AB = MergeSymDiff<A, B>; // { b: 2, c: 4 }
type BA = AB;MergeSymDiffDeep
type MergeSymDiffDeep<A, B>Merge two objects deeply, keeping all non-shared keys and recursively merges shared keys when both are objects.
type A = { a: 1, b: { c: 1, d: "b" } };
type B = { b: { d: "b" } };
type AB1 = MergeDiff<A, B>; // { a: 1 }
type AB2 = MergeDiffDeep<A, B>; // { a: 1, b: { c: 1 } }Merge
type Merge<
A,
B,
M extends
| "int"
| "int-preserve"
| "union"
| "union-preserve"
| "patch"
| "patch-preserve"
| "diff"
| "sym-diff" = "union",
>Merge two objects using the given method. (default: "union")
See MergeUnion, MergePatch, MergeInt, MergeDiff and MergeSymDiff
MergeDeep
type MergeDeep<
A,
B,
M extends
| "patch"
| "patch-preserve"
| "union"
| "union-preserve"
| "int"
| "int-preserve"
| "diff"
| "sym-diff" = "union",
>Merge two objects deeply using the specified method. (default: "union")
modifiers
Mutable
type Mutable<T>Makes all properties of T mutable.
RequiredDeep
type RequiredDeep<T>Makes all properties of T required (deep).
PartialDeep
type PartialDeep<T>Makes all properties of T optional (deep).
ReadonlyDeep
type ReadonlyDeep<T>Makes all properties of T readonly (deep).
MutableDeep
type MutableDeep<T>Makes all properties of T mutable (deep).
RequiredKey
type RequiredKey<T, K extends Keys<Known<T>>>Makes a property of T required.
PartialKey
type PartialKey<T, K extends Keys<Known<T>>>Makes a property of T optional.
ReadonlyKey
type ReadonlyKey<T, K extends Keys<Known<T>>>Makes a property of T readonly.
MutableKey
type MutableKey<T, K extends Keys<Known<T>>>Makes a property of T mutable.
pick
PickDeep
type PickDeep<T, K extends SuggestKey<KeysDeep<Known<T>>>>Pick deeply nested properties using dot-separated paths.
type Input = { a: 1, b: { c: 4, cc: 500 }, e: { door: { f: 5, ff: true } } };
type Result = PickDeep<Input, "a" | "b.c" | "e.door.ff">; // { a: 1, b: { c: 4 }, e: { doo: { ff: true } } }OmitDeep
type OmitDeep<T, K extends SuggestKey<KeysDeep<Known<T>>>>Omit deeply nested properties using dot-separated paths
type Input = { a: 1, b: { c: 4, cc: 500 }, e: { door: { f: 5, ff: true } } };
type Result = OmitDeep<Input, "a" | "b.c" | "e.door.ff">; // { b: { cc: 500 }, e: { doo: { f: 5 } } }LoosePick
type LoosePick<T, K extends SuggestKey<Keys<Known<T>>>>A distributive/loose version of the built-in Pick.
type R1 = Pick<{a: 1}, "a" | "b">; // { a: 1, b: unknown }
type R2 = LoosePick<{a: 1}, "a" | "b">; // { a: 1 }StrictPick
type StrictPick<T, K extends Keys<T>>A distributive version of the built-in Pick.
type R1 = Pick<{a: 1} | {b: 2}, "a">; // { a: unknown } // + Error
type R2 = StrictPick<{a: 1} | {b: 2}, "a">; // { a: 1 } | {}LooseOmit
type LooseOmit<T, K extends SuggestKey<Keys<Known<T>>>>A distributive version of the built-in Omit.
type R1 = Omit<{a: 1, b: 2}, "a">; // { b: 2 }
type R2 = LooseOmit<{a: 1, b: 2}, "a">; // { b: 2 } // + Auto-completion for 'a'
type R3 = Omit<{a: 1} | {b: 2}, "a">; // {}
type R4 = LooseOmit<{a: 1} | {b: 2}, "a">; // {} | {b: 2}StrictOmit
type StrictOmit<T, K extends Keys<T>>A distributive/strict version of the built-in Omit.
type R1 = Omit<{a: 1}, "z">; // {a: 1}
type R2 = StrictOmit<{a: 1}, "z">; // {a: 1} // + Error saying 'z' is not an existing key
type R3 = Omit<{a: 1} | {b: 2}, "a">; // {} // + Error
type R4 = StrictOmit<{a: 1} | {b: 2}, "a">; // {} | {b: 2}PickOptional
type PickOptional<T>Pick optional properties of T.
type R1 = PickOptional<{ a: 1, b: 2, c?: 4 }>; // { c?: 4 }PickOptionalDeep
type PickOptionalDeep<T>Recursively pick optional properties of T.
type Input = { a?: 1, b: { c?: 4, d: 5 } }
type R1 = PickOptional<Input>; // { a?: 1 }
type R2 = PickOptionalDeep<Input>; // { a?: 2, b: { c?: 4 } }PickRequired
type PickRequired<T>Pick required properties of T.
type R1 = PickRequired<{ a: 1, b: 2, c?: 4 }>; // { a: 1, b: 2 }PickRequiredDeep
type PickRequiredDeep<T>Recursively pick required properties of T.
type Input = { a?: 1, b: { c?: 4, d: 5 } }
type R1 = PickRequired<Input>; // { b: { c?: 4, d: 5 } }
type R2 = PickRequiredDeep<Input>; // { b: { d: 5 } }PickReadonly
type PickReadonly<T>Pick readonly properties of T.
type R1 = PickReadonly<{ a: 1, readonly c: 4 }>; // { readonly c: 4 }PickReadonlyDeep
type PickReadonlyDeep<T>Recursively pick readonly properties of T.
type Input = { readonly a: 1; b: { readonly c: 4, d: 5 }, e: 6 };
type R1 = PickReadonly<Input>; // { readonly a: 1 }
type R2 = PickReadonlyDeep<Input>; // { readonly a: 1, b: { readonly c: 4 } }PickMutable
type PickMutable<T>Pick mutable properties of T.
type R1 = PickMutable<{ a: 1, readonly c: 4 }>; // { a: 1 }PickMutableDeep
type PickMutableDeep<T>Recursively pick mutable properties of T.
type Input = { readonly a: 1; b: { readonly c: 4, d: 5 }, e: 6 };
type R1 = PickMutable<Input>; // { b: { readonly c: 4, d: 5 }, e: 6 }
type R2 = PickMutableDeep<Input>; // { b: { d: 5 }, e: 6 }prefix
PrefixKeys
type PrefixKeys<T, P extends string>Adds a prefix to all keys of T.
PrefixKeysEnsure
type PrefixKeysEnsure<T, P extends string>Adds a prefix to all keys of T, but only if the key does not already start with the prefix.
UnPrefixKeys
type UnPrefixKeys<T, P extends string>Removes a prefix from all keys of T, if it exists.
SuffixKeys
type SuffixKeys<T, S extends string>Adds a suffix to all keys of T.
SuffixKeysEnsure
type SuffixKeysEnsure<T, S extends string>Adds a suffix to all keys of T, but only if the key does not already end with the suffix.
UnSuffixKeys
type UnSuffixKeys<T, S extends string>Removes a suffix from all keys of T, if it exists.
rename
RenameKey
type RenameKey<T, K extends Keys<Known<T>>, N extends DictKey>Renames a key of T.
type R1 = RenameKey<{rgb: RGB, a: number}, "a", "alpha">; // { rgb: RGB, alpha: number }RenameKeys
type RenameKeys<T, M extends Partial<Dict<Keys<Known<T>>, DictKey>>>Renames multiple keys of T using a key-to-new-key object.
type R1 = RenameKeys<{f: string, b: string, c: 4}, {f: "foo", b: "bar"}>; // { foo: string, bar: string, c: 4 }utils
DictKey
type DictKeyTypes that can be used as keys for an object.
Dict
type Dict<K extends DictKey = string, V = unknown>An alias for Record<K, V> with defaults.
Known
type Known<T>Removes the index signature from an object.
type Config = { foo: string, bar: boolean, [x: string]: string };
type KnownConfig = Known<Config>; // { foo: string, bar: boolean }KnownDeep
type KnownDeep<T>Recursively removes the index signature from nested objects.
type A = {
a: 1;
b: {
c: 4;
[x: string]: number;
};
d: {
ak: 47;
[x: string]: number;
};
};
type A1 = Known<A>; // A
type A2 = KnownDeep<A>; // { a: 1, b: { c: 4 }, d: { ak: 47 } }SuggestKey
type SuggestKey<T, S extends boolean = false>Generates a type that suggests a pre-defined list of dictionary keys while accepting any other string or number.
Prettify
type Prettify<T>Prettifies an object.
type R1 = Readonly<{ a: 1 } | { b: 2 }>; // Readonly<{ a: 1 }> | Readonly<{ b: 2 }>
type R2 = Prettify<R1>; // { readonly a: 1 } | { readonly b: 2 }PrettifyDeep
type PrettifyDeep<T>A deep version of Prettify.
PrettifyKeyof
type PrettifyKeyof<T>type MyDict = { a: 1, b: 2 };
type MyDictKeys = keyof MyDict; // type: keyof MyDict
type Pretty = PrettifyKeyof<keyof MyDict>; // type: "a" | "b"