DEV Community 👩‍💻👨‍💻

Discussion on: I need to learn about TypeScript Template Literal Types

malcolmkee profile image
Malcolm Kee

One question about PathValue. With your implementation above, we get never with get(user, '').

But it will get the correct type with one small change:

type Path<T, Key extends keyof T = keyof T> =
  (Key extends string
  ? T[Key] extends Record<string, any>
    ? | `${Key}.${Path<T[Key], Exclude<keyof T[Key], keyof Array<any>>> & string}`
      | `${Key}.${Exclude<keyof T[Key], keyof Array<any>> & string}`
+      | Key
-       | never
    : never
  : never)
Enter fullscreen mode Exit fullscreen mode

See here.