@@ -150,8 +150,18 @@ export type DeepKeysAndValuesImpl<
150150 ? DeepKeyAndValueObject < TParent , T , TAcc >
151151 : TAcc
152152
153- export type DeepRecord < T > = {
154- [ TRecord in DeepKeysAndValues < T > as TRecord [ 'key' ] ] : TRecord [ 'value' ]
153+ type DeepRecordWithDynamicSuffix < T > = {
154+ // DeepKeys uses a dot as reserved character, so the only way that it can be a suffix
155+ // is if the suffix is a dynamic variable.
156+ [ TRecord in DeepKeysAndValues < T > as `${TRecord [ 'key' ] } .` extends TRecord [ 'key' ]
157+ ? TRecord [ 'key' ]
158+ : never ] : TRecord [ 'value' ]
159+ }
160+
161+ type DeepRecordWithStaticSuffix < T > = {
162+ [ TRecord in DeepKeysAndValues < T > as `${TRecord [ 'key' ] } .` extends TRecord [ 'key' ]
163+ ? never
164+ : TRecord [ 'key' ] ] : TRecord [ 'value' ]
155165}
156166
157167/**
@@ -161,19 +171,22 @@ export type DeepKeys<T> = unknown extends T
161171 ? string
162172 : DeepKeysAndValues < T > [ 'key' ]
163173
164- /**
165- * Infer the type of a deeply nested property within an object or an array.
166- */
167- export type DeepValue < TValue , TAccessor > = unknown extends TValue
168- ? TValue
169- : TAccessor extends DeepKeys < TValue >
170- ? DeepRecord < TValue > [ TAccessor ]
171- : never
172-
173174/**
174175 * The keys of an object or array, deeply nested and only with a value of TValue
175176 */
176177export type DeepKeysOfType < TData , TValue > = Extract <
177178 DeepKeysAndValues < TData > ,
178179 AnyDeepKeyAndValue < string , TValue >
179180> [ 'key' ]
181+
182+ type PickValue < T , K extends keyof T > = T [ K ]
183+ /**
184+ * Infer the type of a deeply nested property within an object or an array.
185+ */
186+ export type DeepValue < TValue , TAccessor > = unknown extends TValue
187+ ? TValue
188+ : TAccessor extends keyof DeepRecordWithStaticSuffix < TValue >
189+ ? PickValue < DeepRecordWithStaticSuffix < TValue > , TAccessor >
190+ : TAccessor extends keyof DeepRecordWithDynamicSuffix < TValue >
191+ ? PickValue < DeepRecordWithDynamicSuffix < TValue > , TAccessor >
192+ : never
0 commit comments