File tree Expand file tree Collapse file tree 5 files changed +75
-4
lines changed
Expand file tree Collapse file tree 5 files changed +75
-4
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,32 @@ const obj = { foo: { bar: 123, baz: 456 } };
5353type NestedKeys = DotNestedKeys <typeof obj >; // "foo.bar" | "foo.baz"
5454```
5555
56+ ### ``` ChildItemType<T, key> ```
57+ #### Example
58+ ``` ts
59+ // Example usage
60+ interface Obj {
61+ foo: string [];
62+ bar: { baz: number };
63+ baz: Array <{id: number , name: string }>;
64+ }
65+ type FooItem = ChildItemType <Obj , " foo" > // string
66+ type BarItem = ChildItemType <Obj , " bar" > // { baz: number }
67+ type BazItem = ChildItemType <Obj , " baz" > // { id: number, name: string }
68+ ` ` `
69+
70+ ### ` ` ` ChildType <T , key >` ` `
71+ #### Example
72+ ` ` ` ts
73+ // Example usage
74+ interface Obj {
75+ foo: string [];
76+ bar: { baz: number };
77+ }
78+ type FooItem = ChildType <Obj , " foo" > // string[]
79+ type BarItem = ChildType <Obj , " bar" > // { baz: number }
80+ ` ` `
81+
5682## Contributing
5783
5884Contributions are welcome! If you find any issues or have any suggestions, please open an issue or submit a pull request.
Original file line number Diff line number Diff line change 1- import { DotNestedKeys } from "./dotNestedKey" ;
1+ import { DotNestedKeys } from "./utils/dotNestedKey" ;
2+
3+ import { ChildItemType , ChildType } from "./utils/childType" ;
24
35export {
4- DotNestedKeys
5- }
6+ DotNestedKeys ,
7+ ChildItemType ,
8+ ChildType
9+ }
Original file line number Diff line number Diff line change 11{
22 "name" : " ob-scan-ts" ,
3- "version" : " 1.0.1 " ,
3+ "version" : " 1.0.2 " ,
44 "description" : " Nested object to typescript interface" ,
55 "main" : " dist/index.js" ,
66 "types" : " dist/index.d.ts" ,
Original file line number Diff line number Diff line change 1+ /**
2+ * The type of the child item.
3+ *
4+ * @template T - The type of the parent object.
5+ * @template key - The key of the child item.
6+ * @returns The type of the child item. If the child item is an array, the type of its elements is returned.
7+
8+ * @example
9+ * // Example usage
10+ * interface Obj {
11+ * foo: string[];
12+ * bar: { baz: number };
13+ * baz: Array<{id: number, name: string}>;
14+ * }
15+ *
16+ * type FooItem = ChildItemType<Obj, "foo"> // string
17+ * type BarItem = ChildItemType<Obj, "bar"> // { baz: number }
18+ * type BazItem = ChildItemType<Obj, "baz"> // { id: number, name: string }
19+ */
20+ export type ChildItemType < T , key extends keyof T > = T [ key ] extends Array < any > ? T [ key ] [ number ] : T [ key ] ;
21+
22+
23+
24+ /**
25+ * The type of the child.
26+ *
27+ * @template T - The type of the parent object.
28+ * @template key - The key of the child item.
29+ * @returns The type of the child item. If the child item is an array, the type of its elements is returned.
30+ * @example
31+ * // Example usage
32+ * interface Obj {
33+ * foo: string[];
34+ * bar: { baz: number };
35+ * }
36+ *
37+ * type FooItem = ChildType<Obj, "foo"> // string[]
38+ * type BarItem = ChildType<Obj, "bar"> // { baz: number }
39+ */
40+ export type ChildType < T , key extends keyof T > = T [ key ] ;
41+
File renamed without changes.
You can’t perform that action at this time.
0 commit comments