|
| 1 | +import type {BuildObject, BuildTuple, ToString} from './internal'; |
| 2 | +import type {Paths} from './paths'; |
| 3 | +import type {Simplify} from './simplify.d'; |
| 4 | +import type {UnionToIntersection} from './union-to-intersection.d'; |
| 5 | +import type {UnknownArray} from './unknown-array'; |
| 6 | +import type {UnknownRecord} from './unknown-record.d'; |
| 7 | + |
| 8 | +/** |
| 9 | +Pick properties from a deeply-nested object. |
| 10 | +
|
| 11 | +It supports recursing into arrays. |
| 12 | +
|
| 13 | +Use-case: Distill complex objects down to the components you need to target. |
| 14 | +
|
| 15 | +@example |
| 16 | +``` |
| 17 | +import type {PickDeep, PartialDeep} from 'type-fest'; |
| 18 | +
|
| 19 | +type Configuration = { |
| 20 | + userConfig: { |
| 21 | + name: string; |
| 22 | + age: number; |
| 23 | + address: [ |
| 24 | + { |
| 25 | + city1: string; |
| 26 | + street1: string; |
| 27 | + }, |
| 28 | + { |
| 29 | + city2: string; |
| 30 | + street2: string; |
| 31 | + } |
| 32 | + ] |
| 33 | + }; |
| 34 | + otherConfig: any; |
| 35 | +}; |
| 36 | +
|
| 37 | +type NameConfig = PickDeep<Configuration, 'userConfig.name'>; |
| 38 | +// type NameConfig = { |
| 39 | +// userConfig: { |
| 40 | +// name: string; |
| 41 | +// }; |
| 42 | +
|
| 43 | +// Supports optional properties |
| 44 | +type User = PickDeep<PartialDeep<Configuration>, 'userConfig.name' | 'userConfig.age'>; |
| 45 | +// type User = { |
| 46 | +// userConfig?: { |
| 47 | +// name?: string; |
| 48 | +// age?: number; |
| 49 | +// }; |
| 50 | +// }; |
| 51 | +
|
| 52 | +// Supports array |
| 53 | +type AddressConfig = PickDeep<Configuration, `userConfig.address.0`>; |
| 54 | +// type AddressConfig = { |
| 55 | +// userConfig: { |
| 56 | +// address: [{ |
| 57 | +// city1: string; |
| 58 | +// street1: string; |
| 59 | +// }]; |
| 60 | +// }; |
| 61 | +// } |
| 62 | +
|
| 63 | +// Supports recurse into array |
| 64 | +type Street = PickDeep<Configuration, `userConfig.address.1.street2`>; |
| 65 | +// type AddressConfig = { |
| 66 | +// userConfig: { |
| 67 | +// address: [ |
| 68 | +// unknown, |
| 69 | +// {street2: string} |
| 70 | +// ]; |
| 71 | +// }; |
| 72 | +// } |
| 73 | +``` |
| 74 | +
|
| 75 | +@category Object |
| 76 | +@category Array |
| 77 | +*/ |
| 78 | +export type PickDeep<T extends UnknownRecord | UnknownArray, PathUnion extends Paths<T>> = |
| 79 | + T extends UnknownRecord |
| 80 | + ? Simplify<UnionToIntersection<{ |
| 81 | + [P in PathUnion]: InternalPickDeep<T, P>; |
| 82 | + }[PathUnion]>> |
| 83 | + : T extends UnknownArray |
| 84 | + ? UnionToIntersection<{ |
| 85 | + [P in PathUnion]: InternalPickDeep<T, P>; |
| 86 | + }[PathUnion] |
| 87 | + > |
| 88 | + : never; |
| 89 | + |
| 90 | +/** |
| 91 | +Pick an object/array from the given object/array by one path. |
| 92 | +*/ |
| 93 | +type InternalPickDeep< |
| 94 | + T extends UnknownRecord | UnknownArray, |
| 95 | + Path extends string | number, // Checked paths, extracted from unchecked paths |
| 96 | +> = |
| 97 | + T extends UnknownArray ? PickDeepArray<T, Path> |
| 98 | + : T extends UnknownRecord ? Simplify<PickDeepObject<T, Path>> |
| 99 | + : never; |
| 100 | + |
| 101 | +/** |
| 102 | +Pick an object from the given object by one path. |
| 103 | +*/ |
| 104 | +type PickDeepObject<RecordType extends UnknownRecord, P extends string | number> = |
| 105 | + P extends `${infer RecordKeyInPath}.${infer SubPath}` |
| 106 | + ? BuildObject<RecordKeyInPath, InternalPickDeep<NonNullable<RecordType[RecordKeyInPath]>, SubPath>, RecordType> |
| 107 | + : P extends keyof RecordType | ToString<keyof RecordType> // Handle number keys |
| 108 | + ? BuildObject<P, RecordType[P], RecordType> |
| 109 | + : never; |
| 110 | + |
| 111 | +/** |
| 112 | +Pick an array from the given array by one path. |
| 113 | +*/ |
| 114 | +type PickDeepArray<ArrayType extends UnknownArray, P extends string | number> = |
| 115 | + // Handle paths that are `${number}.${string}` |
| 116 | + P extends `${infer ArrayIndex extends number}.${infer SubPath}` |
| 117 | + // When `ArrayIndex` is equal to `number` |
| 118 | + ? number extends ArrayIndex |
| 119 | + ? ArrayType extends unknown[] |
| 120 | + ? Array<InternalPickDeep<NonNullable<ArrayType[number]>, SubPath>> |
| 121 | + : ArrayType extends readonly unknown[] |
| 122 | + ? ReadonlyArray<InternalPickDeep<NonNullable<ArrayType[number]>, SubPath>> |
| 123 | + : never |
| 124 | + // When `ArrayIndex` is a number literal |
| 125 | + : ArrayType extends unknown[] |
| 126 | + ? [...BuildTuple<ArrayIndex>, InternalPickDeep<NonNullable<ArrayType[ArrayIndex]>, SubPath>] |
| 127 | + : ArrayType extends readonly unknown[] |
| 128 | + ? readonly [...BuildTuple<ArrayIndex>, InternalPickDeep<NonNullable<ArrayType[ArrayIndex]>, SubPath>] |
| 129 | + : never |
| 130 | + // When the path is equal to `number` |
| 131 | + : P extends `${infer ArrayIndex extends number}` |
| 132 | + // When `ArrayIndex` is `number` |
| 133 | + ? number extends ArrayIndex |
| 134 | + ? ArrayType |
| 135 | + // When `ArrayIndex` is a number literal |
| 136 | + : ArrayType extends unknown[] |
| 137 | + ? [...BuildTuple<ArrayIndex>, ArrayType[ArrayIndex]] |
| 138 | + : ArrayType extends readonly unknown[] |
| 139 | + ? readonly [...BuildTuple<ArrayIndex>, ArrayType[ArrayIndex]] |
| 140 | + : never |
| 141 | + : never; |
0 commit comments