03 / 03

How to extend types?

Difficulty: 5/10
type augmentation, declaration merging, interface extension

Using & intersection. Intersection types are closely related to union types, but they are used very differently. An intersection type combines multiple types into one. This allows you to add together existing types to get a single type that has all the features you need.

javascript

Scenario Questions

0-2 years experience

  1. 1We have a function that takes a parameter of type `User` with fields `id` and `name`. We now need to add an optional `email` field without modifying the original `User` definition. How would you extend the type?
  2. 2Given an interface `Config` with properties `url: string` and `timeout: number`, you need to create a new type that includes all `Config` properties plus a `retries?: number`. Show the TypeScript code you would write.

2-5 years experience

  1. 1Our codebase uses a third‑party library that declares an interface `RequestOptions`. We need to add a custom `authToken` property for our internal usage without forking the library. How would you extend the type, and what pitfalls should you watch for?
  2. 2You refactored a component to accept a generic `Props` type, but later you need to add a new optional prop `theme` that should be available to all components. Explain how you would extend the existing props type and why a simple intersection might cause issues.

5-8 years experience

  1. 1We're building a shared UI component library used across several services. Each service wants to augment the base `ButtonProps` with its own styling fields. How would you design the type extensions to allow safe augmentation while preventing breaking changes for existing consumers?
  2. 2During a migration from a legacy JavaScript module to TypeScript, you need to add additional fields to a type exported by the legacy module without editing its source. Discuss how you would use declaration merging or module augmentation, and the trade‑offs regarding build time and type safety.

8+ years experience

  1. 1Our organization is standardizing on a core domain model defined in a central package. Multiple product teams need to extend these domain types with product‑specific attributes. How would you structure the type extensions and package boundaries to support independent evolution while keeping a single source of truth?
  2. 2We plan to deprecate a set of internal types and replace them with a new version across dozens of microservices. Describe a strategy using TypeScript's module augmentation and versioned type namespaces to roll out the change gradually without breaking existing services.

Follow-up Questions

  • What happens if another part of the code also declares the same property?
  • How would you ensure backward compatibility when adding the new field?
  • Can you describe any runtime implications of these type extensions?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.