QA
Senior LevelJavaScript & TypeScript

How do Recursive Conditional Types work in TypeScript (e.g. JSONValue, DeepReadonly)?

❓ Interview Prompt: Explain recursive type resolution, recursion depth limits, and implementing DeepReadonly and DeepPartial.

Recursive Conditional Types reference themselves within their own definition, allowing type transformations across arbitrary nesting depths of objects and arrays.

Applications:

•
`DeepReadonly<T>`: Recursively applies readonly to object properties and nested array elements.
•
`DeepPartial<T>`: Makes all nested object fields optional.
•
`JSONValue`: Defines valid recursive JSON data shapes.

Code Implementation Example

typescript
// Recursive DeepReadonly implementation
type DeepReadonly<T> = T extends (...args: any[]) => any
  ? T
  : T extends object
  ? { readonly [K in keyof T]: DeepReadonly<T[K]> }
  : T;

interface UserProfile {
  name: string;
  meta: {
    tags: string[];
    settings: { theme: string };
  };
}

const user: DeepReadonly<UserProfile> = {
  name: 'Alice',
  meta: { tags: ['admin'], settings: { theme: 'dark' } }
};

// user.meta.settings.theme = 'light'; // ❌ Compile Error! Nested property is read-only!
💡Recursive DeepReadonly utility type protecting deeply nested object properties.
🎯 Interview Key Takeaway

Recursive conditional types allow deep object transformations like DeepReadonly and DeepPartial across arbitrary object depths.

#TypeScript#Recursion#DeepReadonly#Senior
Back to all JavaScript & TypeScript Questions→

More JavaScript & TypeScript Interview Questions

Beginner LevelJavaScript & TypeScript

What are the differences between var, let, and const in JavaScript?

Beginner LevelJavaScript & TypeScript

What are the JavaScript data types and the difference between primitives and objects?

Beginner LevelJavaScript & TypeScript

What is the difference between == (loose equality) and === (strict equality)?