@@ -19,6 +19,11 @@ type Try<A1, A2, Catch = never> = A1 extends A2 ? A1 : Catch
1919 */
2020export type Narrow < A > = Try < A , [ ] , NarrowRaw < A > >
2121
22+ type END = '\0'
23+ type StripEnd < T extends string > = T extends `${infer Rest } ${END } ` ? Rest : T
24+ type AddEnd < T extends string > = `${T } ${END } `
25+ type AddEndIfDynamic < T extends string > = AddEnd < T > extends T ? AddEnd < T > : T
26+
2227export interface AnyDeepKeyAndValue <
2328 K extends string = string ,
2429 V extends any = any ,
@@ -157,37 +162,37 @@ export type DeepKeys<T> = unknown extends T
157162 ? string
158163 : DeepKeysAndValues < T > [ 'key' ]
159164
160- type ValueOfKey <
161- TValue extends AnyDeepKeyAndValue ,
162- TAccessor extends string ,
163- > = TValue extends any
164- ? TAccessor extends TValue [ 'key' ]
165- ? TValue
166- : never
167- : never
165+ type DeepRecord < T > = {
166+ [ K in DeepKeysAndValues < T > as AddEndIfDynamic < K [ 'key' ] > ] : K [ 'value' ]
167+ }
168168
169- type Maximals <
170- TValue extends AnyDeepKeyAndValue ,
171- All extends AnyDeepKeyAndValue = TValue ,
172- > = TValue extends any
173- ? [ All ] extends [ TValue ]
174- ? TValue
175- : Extract <
176- All ,
177- {
178- key : `${TValue [ 'key' ] } .${string } ` | `${TValue [ 'key' ] } [${string } `
179- }
180- > extends never
181- ? TValue
182- : never
183- : never
169+ type DeepValueImpl <
170+ TDeepRecord extends DeepRecord < unknown > ,
171+ TAccessor extends string ,
172+ > = TAccessor extends keyof TDeepRecord
173+ ? TDeepRecord [ TAccessor ]
174+ : TDeepRecord [ AddEnd < TAccessor > ]
184175
185176/**
186177 * Infer the type of a deeply nested property within an object or an array.
187178 */
188179export type DeepValue < TValue , TAccessor extends string > = unknown extends TValue
189180 ? TValue
190- : Maximals < ValueOfKey < DeepKeysAndValues < TValue > , TAccessor > > [ 'value' ]
181+ : DeepValueImpl < DeepRecord < TValue > , TAccessor >
182+
183+ type Foo = {
184+ a : string
185+ b : number
186+ c : { d : string }
187+ }
188+ type RecordExample = {
189+ records : Record < string , Foo >
190+ }
191+
192+ type E1 = DeepKeys < RecordExample >
193+ type E2 = DeepRecord < RecordExample >
194+
195+ type FooValue2 = DeepValue < RecordExample , 'records.something' >
191196
192197/**
193198 * The keys of an object or array, deeply nested and only with a value of TValue
0 commit comments