Last active
October 13, 2024 12:51
-
-
Save Arkellys/9864919e63d91a1a50571d65f6fa91e7 to your computer and use it in GitHub Desktop.
JSDoc types, they did the job at some point
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @import { Merge, ConditionalPick, SharedUnionFieldsDeep } from "type-fest"; | |
| */ | |
| /** | |
| * Renames keys from `T` with names specified in `R`. | |
| * @example | |
| * Rename.<{ a: 1, b: 2 } | { a: 3 }, { a: "d" }> | |
| * π‘ { d: 1, b: 2 } | { d: 3 } | |
| * @template {Record.<any, any>} T - Type to pick the keys from | |
| * @template {{ [K in keyof R]: K extends keyof T ? PropertyKey : "keyof T" }} R - Association of keys and new names | |
| * @typedef {{ [P in keyof T as P extends keyof R ? R[P] : P]: T[P] }} Rename | |
| */ | |
| /** | |
| * Picks values from `T` and rename them with names specified in `R`. | |
| * @example | |
| * PickRename.<{ a: 1, b: 2 } | { a: 3 }, { a: "d" }> | |
| * π‘ { d: 1 } | { d: 3 } | |
| * @template {Record.<any, any>} T - Type to pick the keys from | |
| * @template {{ [K in keyof R]: K extends keyof T ? PropertyKey : "keyof T" }} R - Association of keys to pick and new names | |
| * @typedef {LoosePick.<Rename.<T, R>, ValueOf.<R>>} PickRename | |
| */ | |
| /** | |
| * Gets the keys and nested keys of `T`. | |
| * @example | |
| * DeepKeyOf<{ a: 1, b: { c: 2, d: { e: 3 } } }> | |
| * π‘ "b" | "e" | "d" | "a" | "c" | |
| * @template {Record<any, any>} T - Type to get the values of | |
| * @typedef {T extends T | |
| * ? { [K in keyof T]: T[K] extends Record<any, any> ? DeepKeyOf<T[K]> : keyof T }[keyof T] | |
| * : never | |
| * } DeepKeyOf | |
| */ | |
| /** | |
| * Constructs a type with the properties of `T` except for those of type `never`. | |
| * @example | |
| * OmitNever<{ a: string, b: number, c: never }> | |
| * π‘ { a: string, b: number } | |
| * @template {any} T - Type to omit the properties from | |
| * @typedef {{ [K in keyof T as IsNever<T[K]> extends true ? never : K]: T[K] }} OmitNever | |
| */ | |
| /** | |
| * Replaces the properties assignable to `U` with `R` from all unions of `T`. | |
| * @example | |
| * Replace<{ a: 1, b: 2, c: [1, 2], d: { e: 1, f: { g: 2, h: 1 }[] } } | { i: 1, j: 2 }, 2, 3> | |
| * π‘ { a: 1, b: 3, c: [1, 2], d: { e: 1, f: { g: 3, h: 1 }[] } } | { i: 1, j: 3 } | |
| * @template {any} T - Type to exclude the properties from | |
| * @template {any} U - Type to exclude the properties with | |
| * @template {any} R - Type to replace with | |
| * @typedef {T extends T | |
| * ? T extends Record<any, any> | |
| * ? { [K in keyof T ]: T[K] extends U ? R : Replace<T[K], U, R> } | |
| * : T | |
| * : never | |
| * } Replace | |
| */ | |
| /** | |
| * Gets the paths of `K` in `T`. | |
| * @example | |
| * RecursivePaths<{ a: 1, b: { c: 2, d: { e: 3 }[] }, f: [4, 5] }, "b"> | |
| * π‘ "b.c" | "b.d" | `b.d.${number}` | `b.d.${number}.e` | |
| * @template {any} T - Type to get the sub-paths from | |
| * @template {keyof T} K - Property to get the paths from | |
| * @template {any} [R=Paths<T[K]>] - Recursive type | |
| * @typedef {IsNever<R> extends false | |
| * ? K extends (string | number) | |
| * ? `${K}.${R extends (string | number) ? `${R}` : never}` | |
| * : never | |
| * : never | |
| * } RecursivePaths | |
| */ | |
| /** | |
| * Gets the possible paths of all unions of `T`. | |
| * @example | |
| * Paths<{ a: 1, b: { c: 2, d: { e: 3 }[] }, f: [4, 5] }> | |
| * π‘ "a" | "b" | "f" | "b.d" | "b.c" | `b.d.${number}` | `b.d.${number}.e` | "f.0" | "f.1" | |
| * @template {any} T - Type to get the paths from | |
| * @typedef {T extends T | |
| * ? T extends Record<any, any> | |
| * ? { [K in keyof T]: (K | RecursivePaths<T, K>) }[T extends any[] ? (number & keyof T) : keyof T] | |
| * : never | |
| * : never | |
| * } Paths | |
| */ | |
| /** | |
| * Tests whether `T` is of the exact type `any`. | |
| * @example | |
| * IsAny<any> | |
| * π‘ true | |
| * @template {any} T - Type to test | |
| * @typedef {0 extends (1 & T) ? true : false} IsAny | |
| */ | |
| /** | |
| * Tests whether `T` is of the exact type `never`. | |
| * @example | |
| * IsNever<never> | |
| * π‘ true | |
| * @template {any} T - Type to test | |
| * @typedef {[T] extends [never] ? true : false} IsNever | |
| */ | |
| /** | |
| * Constructs a type by removing properties `K` from all unions of `T`. | |
| * @example | |
| * DistributiveOmit<({ a: 1, b: 2 } | { a: 3, c: 4 }) & { d: 5 }, "a"> | |
| * π‘ ({ b: 2 } | { c: 4 }) & { d: 5 } | |
| * @template {Record<any, any>} T - Type to omit the properties from | |
| * @template {PropertyKey} K - Properties to omit | |
| * @typedef {T extends T ? Omit<T, K> : never} DistributiveOmit | |
| */ | |
| /** | |
| * Constructs a type by picking properties `K` from all unions of `T`. | |
| * @example | |
| * DistributivePick<{ a?: string, b?: false } | { a: string, b: true, c: 3 }, "a" | "b"> | |
| * π‘ { a?: string, b?: false } | { a: string, b: true } | |
| * @template {Record<any, any>} T - Type to pick the properties from | |
| * @template {PropertyKey} K - Properties to pick | |
| * @typedef {T extends T ? Pick<T, K> : never} DistributivePick | |
| */ | |
| /** | |
| * Gets the values of `T`. | |
| * @example | |
| * ValueOf<{ a: 1, b: 2, c: 3 }> | |
| * π‘ 1 | 2 | 3 | |
| * @template {Record<any, any>} T - Type to get the values of | |
| * @typedef {T[keyof T]} ValueOf | |
| */ | |
| /** | |
| * Overwrites properties from `A` with matching properties from `B`. | |
| * @example | |
| * Overwrite<{ a: 1, b: 2 }, { a: 3 }> | |
| * π‘ { a: 3, b: 2 } | |
| * @template {Record<any, any>} A - Type to overwrite | |
| * @template {Record<any, any>} B - Type to overwrite with | |
| * @typedef {{ [P in keyof A]: P extends keyof B ? B[P] : A[P] }} Overwrite | |
| */ | |
| /** | |
| * Constructs a type by picking the set of properties of value type `V` from `T`. | |
| * @example | |
| * PickByValue<{ a: number, b: string, c: number | string }, number> | |
| * π‘ { a: number, c: number | string } | |
| * @template {Record<any, any>} T - Type to get the properties from | |
| * @template {any} V - Type of value to test the properties with | |
| * @typedef {Pick<T, { [P in keyof T]: T[P] extends V ? P : never }[keyof T]>} PickByValue | |
| */ | |
| /** | |
| * Constructs a type by picking the set of properties `K` from `T`, ignores if `T` is not a key of `T`. | |
| * @example | |
| * LoosePick<{ a: 1, b: 2, c: 3 }, "a" | "d"> | |
| * π‘ { a: 1 } | |
| * @template {Record<any, any>} T - Type to pick the keys from | |
| * @template {PropertyKey} K - Keys to pick | |
| * @typedef {Omit<T, keyof Omit<T, K>>} LoosePick | |
| */ | |
| /** | |
| * Gets the values and nested values of `T`. | |
| * @example | |
| * DeepValueOf<{ a: 1, b: { c: 2, d: { e: 3 } } }> | |
| * π‘ 1 | 2 | 3 | |
| * @template {Record<any, any>} T - Type to get the values of | |
| * @typedef {T extends T | |
| * ? { [K in keyof T]: T[K] extends Record<any, any> ? DeepValueOf<T[K]> : T[K] }[keyof T] | |
| * : never | |
| * } DeepValueOf | |
| */ | |
| /** | |
| * Merges properties of union types `T` into a single type. | |
| * @example | |
| * MergeUnion<{ a: 1, b: 2 } | { a: 3, b: 4 } | { a: 5, c: 6 }> | |
| * π‘ { a: 1 | 2 | 4, b?: 2 | 4, c?: 6 } | |
| * @template {any} T - Union type to merge | |
| * @typedef {IsUnion<T> extends true | |
| * ? SharedUnionFieldsDeep<Or<T>> | |
| * : T | |
| * } MergeUnion | |
| */ | |
| /** | |
| * Flattens shallowly values of type `T`. | |
| * @example | |
| * ShallowFlatten<{ a: 1, b: 2[], c: { d: 3 } | { e: 4 }, d: ({ f: 5 } | { f: 6, g: 7 })[] }> | |
| * π‘ { a: 1, b: 2, c: { d?: 3, e?: 4 }, d: { f: 5 | 6, g?: 7 } } | |
| * @template {Record<any, any>} T - Type to flatten | |
| * @typedef {T extends T | |
| * ? { [K in keyof T]: MergeUnion<T[K] extends any[] ? T[K][0] : T[K]>} | |
| * : never | |
| * } ShallowFlatten | |
| */ | |
| /** | |
| * Constructs a type by picking the set of properties from `T` whose values contain the property `K`. | |
| * @example | |
| * PickByNestedProperty<{ a: 1, b: { c: 2 }, d: { t: 3 }, e: { t: 4 }[], f: { t: 5 } | { g: 6 }, h: ({ i: 7 } | { t: 8, i: 9 })[] }> | |
| * π‘ { d: { t: 3 }, e: { t: 4 }[], f: { t: 5 } | { g: 6 }, h: ({ i: 7 } | { t: 8, i: 9 })[] } | |
| * @template {Record<any, any>} T - Type to pick from | |
| * @template {keyof any} K - Name of the property to pick with | |
| * @typedef {Pick<T, keyof ConditionalPick<ShallowFlatten<T>, { [P in K]?: any }>> | |
| * } PickByNestedProperty | |
| */ | |
| /** | |
| * Overwrites values of property `K` in `T` with intersecting values of `V`. | |
| * @example | |
| * OverwriteIntersectingProperty<{ a: "a" | "b" | "c" } | { a: "a" } | { a: "c" }, "a", "a" | "b"> | |
| * π‘ { a: "a" | "b" } | { a: "a" } | |
| * @template {Record<any, any>} T - Type to overwrite the property of | |
| * @template {PropertyKey} K - Name of the property to overwrite | |
| * @template {string} V - Type to overwrite intersecting values of the property's with | |
| * @typedef {T extends T | |
| * ? K extends keyof T | |
| * ? Exclude<Merge<T, { [P in K]: Extract<V, T[K]> }>, { [P in K]: never }> | |
| * : never | |
| * : never | |
| * } OverwriteIntersectingProperty | |
| */ | |
| /** | |
| * Tests whether `T` is an union. | |
| * @example | |
| * IsUnion<"a" | "b" | "c"> | |
| * π‘ true | |
| * @template {any} T - Type ot test | |
| * @template {T} [U=T] - Copy of `T` | |
| * @typedef {T extends unknown ? [U] extends [T] ? false : true : false} IsUnion | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment