Nested Objects in TypeScript
Nested Objects in TypeScript
Section titled “Nested Objects in TypeScript”Basic Nested Types
Section titled “Basic Nested Types”interface Address { street: string; city: string; zipCode: string;}
interface Employee { id: number; name: string; address: Address; department: { id: number; name: string; };}Optional Nesting with Optional Chaining
Section titled “Optional Nesting with Optional Chaining”interface Company { name: string; headquarters?: { address?: { street?: string; city?: string; }; phone?: string; };}
function getCity(company: Company): string | undefined { return company.headquarters?.address?.city;}Recursive Types
Section titled “Recursive Types”interface TreeNode<T> { value: T; children: TreeNode<T>[];}
interface Comment { id: string; text: string; replies: Comment[];}Interview Questions
Section titled “Interview Questions”Easy: How do you type nested objects?
Medium: How does optional chaining work with nested types?
Hard: How would you create a recursive type for a tree structure?