Skip to content

Commit 126cef1

Browse files
committed
Added commented code for StringPath.
1 parent c66cab6 commit 126cef1

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/compare.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,16 @@ test('Check path existence', () => {
385385
value: true
386386
});
387387
});
388+
389+
/*
390+
test('String paths', () => {
391+
type Social = z.infer<typeof social>;
392+
const i = 5;
393+
const i2 = '5';
394+
395+
const path1: StringPath<Social> = `user.tags.${i}.name`;
396+
const path2: StringPath<Social> = `user.tags.${i2}.name`;
397+
398+
expect(path1).toEqual(path2);
399+
});
400+
*/

src/lib/index.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ export type FormFields<T extends AnyZodObject> = {
139139
};
140140

141141
// eslint-disable-next-line @typescript-eslint/no-explicit-any
142-
export type FieldPath<T extends object> = [
143-
keyof T,
144-
...(string | number | symbol)[]
145-
];
142+
export type FieldPath<T extends object> = [keyof T, ...(string | number)[]];
146143

147144
// Thanks to https://stackoverflow.com/a/71859443/70894
148145
export type FormPath<T, K> = K extends []
@@ -160,3 +157,24 @@ export type FormPath<T, K> = K extends []
160157
: K extends (infer U)[]
161158
? U
162159
: never;
160+
161+
/*
162+
// Thanks to https://stackoverflow.com/a/76062131/70894
163+
type Dot<
164+
BasePath extends string | number,
165+
SubPath extends string
166+
> = '' extends SubPath ? BasePath : `${BasePath}.${SubPath}`;
167+
168+
type DefaultTargetFields = number | string | boolean | Date;
169+
170+
export type StringPath<
171+
Obj,
172+
TargetFields = DefaultTargetFields
173+
> = Obj extends TargetFields
174+
? ''
175+
: Obj extends unknown[]
176+
? Dot<`${number}`, StringPath<Obj[number], TargetFields>>
177+
: {
178+
[K in keyof Obj & string]: Dot<K, StringPath<Obj[K], TargetFields>>;
179+
}[keyof Obj & string];
180+
*/

0 commit comments

Comments
 (0)