@@ -4,28 +4,50 @@ import type { ElementOf } from './ElementOf';
44import type { RemoveArraySymbol } from './RemoveArraySymbol' ;
55
66/**
7- * @title Type to infer all value types of generic T
7+ * Helper type that extracts all value types from an object type.
8+ * Similar to `T[keyof T]`, it returns a union of all property value types.
9+ *
10+ * @internal
11+ * @template T - The object type to extract values from
812 */
913type ValueOf < T > = T [ keyof T ] ;
1014
1115/**
12- * @title The type that pulls out the type of a particular key on an interface .
16+ * @title Type for extracting the type at a specific nested path in an object .
1317 *
14- * This type extracts the type of a specific key from a nested object,
18+ * This type extracts the type of a specific key from a nested object structure ,
1519 * supporting arrays and deeply nested keys. It uses `DeepStrictObjectKeys`
16- * to handle the extraction of keys and correctly resolves the type for the given key.
20+ * to validate the key path and correctly resolves the type for the given key.
21+ *
22+ * Key features:
23+ * - Supports dot notation for nested object access (e.g., "a.b.c")
24+ * - Handles array access with `[*]` notation (e.g., "items[*].name")
25+ * - Type-safe: only accepts valid key paths as defined by `DeepStrictObjectKeys`
26+ * - Recursively resolves nested types through objects and arrays
1727 *
18- * - If the key points to a primitive value, the type is returned directly.
19- * - If the key points to an array, the type of the array elements is resolved.
20- * - It supports nested keys using `.` notation to handle deep objects and arrays.
28+ * @template T - The object type to extract from
29+ * @template K - The key path string (must be a valid key from DeepStrictObjectKeys<T>)
30+ * @returns The type at the specified path, or `never` if the path is invalid
2131 *
22- * @template T The interface type.
23- * @template K The key string, which can represent a nested key path.
32+ * @example
33+ * ```typescript
34+ * type Data = {
35+ * user: {
36+ * name: string;
37+ * posts: {
38+ * title: string;
39+ * tags: string[];
40+ * }[];
41+ * };
42+ * };
2443 *
25- * Example usage:
26- * ```ts
27- * type Example1 = GetType<{ a: { b: { c: number } } }, "a.b">; // { c: number }
28- * type Example2 = GetType<{ a: { b: { c: number } } }, "a.b.c">; // number
44+ * type UserType = GetType<Data, "user">; // { name: string; posts: {...}[] }
45+ * type NameType = GetType<Data, "user.name">; // string
46+ * type PostsType = GetType<Data, "user.posts">; // { title: string; tags: string[] }[]
47+ * type PostType = GetType<Data, "user.posts[*]">; // { title: string; tags: string[] }
48+ * type TitleType = GetType<Data, "user.posts[*].title">; // string
49+ * type TagsType = GetType<Data, "user.posts[*].tags">; // string[]
50+ * type TagType = GetType<Data, "user.posts[*].tags[*]">; // string
2951 * ```
3052 */
3153export type GetType < T extends object , K extends DeepStrictObjectKeys < T > > =
0 commit comments