How to Define Strongly Type Nested Object Keys with Generics in TypeScript ? Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report We will look into the effective use of generics in TypeScript to ensure type safety when working with nested objects. By employing generics, developers can establish strongly typed keys for nested objects, significantly reducing the likelihood of runtime errors and enhancing code maintainability. Table of Content Using Recursive GenericsUsing Conditional TypesApproach 1: Using Recursive GenericsThis approach employs recursive generics to define a type, NestedObject<T>, representing nested objects. It iterates over each property K of the input type T. If the property's value is an object, it recursively applies the NestedObject type to ensure type safety for nested objects. Otherwise, it assigns the original type. This approach provides an intuitive way to enforce strong typing for nested object keys. Syntax:type NestedObject<T> = { [K in keyof T]: T[K] extends object ? NestedObject<T[K]> : T[K];};Example: This example shows the use of the above-explained approach. JavaScript // Define a generic type for nested objects type NestedObject<T> = { [K in keyof T]: T[K] extends object ? NestedObject<T[K]> : T[K]; }; // Define a nested object type type User = { id: number; name: string; address: { city: string; zip: number; }; }; // Utilize NestedObject type to // create a strongly typed nested object type TypedUser = NestedObject<User>; // Usage const user: TypedUser = { id: 1, name: "John Doe", address: { city: "New York", zip: 12345, }, }; console.log(user.address.city); Output: New YorkApproach 2: Using Conditional TypesIn this approach, we utilize conditional types to define the NestedObject<T> type. It checks if the input type T extends object. If so, it iterates over each property K of T and applies the NestedObject type recursively. Otherwise, it assigns the original type. This approach provides another way to achieve strong typing for nested object keys, offering flexibility in type definitions. Example: This example shows the use of the above-explained approach. JavaScript // Define a generic type for nested // objects using conditional types type NestedObject<T> = T extends object ? { [K in keyof T]: NestedObject<T[K]> } : T; // Define a nested object type type Product = { id: number; name: string; details: { price: number; description: string; }; }; // Utilize NestedObject type to // create a strongly typed nested object type TypedProduct = NestedObject<Product>; // Usage const product: TypedProduct = { id: 1, name: "Laptop", details: { price: 99999, description: "High-performance laptop with SSD storage", }, }; console.log(product.details.price); Output: 99999 Comment More infoAdvertise with us Next Article How to Create a Typed Array from an Object with Keys in TypeScript? N nikunj_sonigara Follow Improve Article Tags : TypeScript Similar Reads How to Define Interfaces for Nested Objects in TypeScript ? In TypeScript, defining interfaces for nested objects involves specifying the structure of each level within the object hierarchy. This helps ensure that the nested objects adhere to a specific shape or pattern. Here are step-by-step instructions on how to define interfaces for nested objects in Typ 2 min read How to Define Generic Type for Matching Object Property Types in TypeScript ? In Typescript, efficiently managing object property types is essential for type safety and code maintainability. Several techniques can be used to define generic types that match the properties of the objects as listed and explained below.Table of ContentUsing Record Utility TypeUsing Mappped TypesU 4 min read How to Create Objects with Dynamic Keys in TypeScript ? In TypeScript, objects with dynamic keys are those where the key names are not fixed and can be dynamically determined at runtime. This allows the creation of flexible data structures where properties can be added or accessed using variables, providing more versatile type definitions.These are the f 3 min read How to Declare Object Value Type Without Declaring Key Type in TypeScript ? We will create an object value type without declaring the key type. We can not directly define the type of value we will use different methods for declaring the object value type. These are the following methods for declaring the object value type: Table of Content Using Record Utility TypeUsing Map 4 min read How to Create a Typed Array from an Object with Keys in TypeScript? Creating a typed array from the keys of an object in TypeScript ensures that your application maintains type safety, particularly when interacting with object properties. Here we'll explore different approaches to achieve this using TypeScript's built-in Object methods.Below are the approaches used 3 min read How to Deep Clone an Object & Preserve its Type with TypeScript ? In TypeScript, deep cloning an object by preserving its type consists of retaining the same structure and type information as of original object. Below are the approaches to Deep Clone an Object & Preserve its Type:Table of ContentUsing JSON.stringify and JSON.parseUsing Object.Assign functionUs 3 min read Like