Skip to content

Commit

Permalink
Merge pull request #907 from micalevisk/master
Browse files Browse the repository at this point in the history
fix(types): stop before try to evaluate `any` as a path
  • Loading branch information
kamilmysliwiec committed May 18, 2022
2 parents 759cb3d + 810d2d4 commit d2d8bb0
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/types/path-value.type.ts
@@ -1,5 +1,17 @@
/**
* Evaluates to `true` if `T` is `any`. `false` otherwise.
* (c) https://stackoverflow.com/a/68633327/5290447
*/
type IsAny<T> = unknown extends T
? [keyof T] extends [never]
? false
: true
: false;

export type PathImpl<T, Key extends keyof T> = Key extends string
? T[Key] extends Record<string, any>
? IsAny<T[Key]> extends true
? never
: T[Key] extends Record<string, any>
?
| `${Key}.${PathImpl<T[Key], Exclude<keyof T[Key], keyof any[]>> &
string}`
Expand All @@ -10,8 +22,10 @@ export type PathImpl<T, Key extends keyof T> = Key extends string
export type PathImpl2<T> = PathImpl<T, keyof T> | keyof T;

export type Path<T> = keyof T extends string
? PathImpl2<T> extends string | keyof T
? PathImpl2<T>
? PathImpl2<T> extends infer P
? P extends string | keyof T
? P
: keyof T
: keyof T
: never;

Expand Down

0 comments on commit d2d8bb0

Please sign in to comment.