TypeScript Generics and Conditional Types in Deeply Nested Objects - How to Access Nested Properties?
I'm working through a tutorial and I've spent hours debugging this and I've encountered a strange issue with I've looked through the documentation and I'm still confused about I've looked through the documentation and I'm still confused about I'm working with a complex API response that consists of deeply nested objects, and I'm trying to access specific properties using TypeScript's generics and conditional types. However, I'm running into issues when trying to define a utility type to extract these nested properties. The API response looks like this: ```json { "user": { "id": 1, "profile": { "name": "John", "settings": { "theme": "dark", "notifications": true } } } } ``` I want to create a utility type that can guide to get the type of `theme` or `notifications` based on the key path I provide. So far, I have tried creating a utility type like this: ```typescript type NestedProperty<T, K> = K extends `${infer Key}.${infer Rest}` ? Key extends keyof T ? NestedProperty<T[Key], Rest> : never : K extends keyof T ? T[K] : never; ``` And I try to use it like this: ```typescript type ThemeType = NestedProperty<typeof apiResponse, 'user.profile.settings.theme'>; ``` However, I'm getting a `Type 'never' is not assignable to type 'string'` behavior when trying to access the `theme`. I've stepped through various attempts to debug this, including making sure the key paths are correct and that my TypeScript version is up-to-date (I'm on 4.5.2). What am I missing in my implementation that could lead to this behavior? Is there a better way to handle deeply nested properties with generics in TypeScript? Any insight would be greatly appreciated! Thanks in advance! For context: I'm using Typescript on Windows. Am I missing something obvious? What are your experiences with this? My development environment is macOS. Thanks for your help in advance! This is for a desktop app running on CentOS. Thanks for your help in advance!