01 / 05

What are interfaces in TypeScript?

Difficulty: 6/10
type contracts, structural typing, declaration merging
  1. 1

    They describe the shape of an object, including its properties and methods.

  2. 2

    They provide a way to implement custom types

  3. 3

    In TS, two types are compatible if their internal structure is compatible. This allows us to implement an interface just by having the shape the interface requires, without an explicit implements clause.

  4. 4

    Not all properties in an interface are required, each optional property is denoted by a ? at the end of the property name in the declaration.

javascript

Scenario Questions

0-2 years experience

  1. 1You need to define a function that takes an object with `id` (number) and `name` (string). How would you use a TypeScript interface to type the parameter?
  2. 2If you have an interface `User` with `email?: string` and you pass an object missing `email`, what does TypeScript do?
  3. 3How would you extend an existing `Person` interface to add an optional `age` property?

2-5 years experience

  1. 1We have a legacy JavaScript module that returns a config object. You need to add type safety without changing its runtime behavior. How would you introduce an interface, and what pitfalls might you encounter?
  2. 2During a refactor, a function that previously accepted a `Partial<User>` started throwing type errors after you added a new required property to `User`. Explain why and how you would fix it.
  3. 3Two components each define their own `Props` interface with overlapping fields but slight differences. How would you consolidate them to avoid duplication while preserving flexibility?

5-8 years experience

  1. 1Our microservice exposes a JSON API that many front‑end teams consume. How would you design TypeScript interfaces to model the API responses, considering versioning and backward compatibility?
  2. 2We have many classes implementing a `Repository` interface, and some implementations need extra methods not in the base interface. How do you handle this without breaking existing consumers?
  3. 3During a performance review, you notice that interfaces with many optional properties cause the compiler to run slowly. What strategies can you use to reduce compile‑time overhead while keeping type safety?

8+ years experience

  1. 1The company is migrating a monorepo from JavaScript to TypeScript. How would you plan the rollout of interfaces across multiple packages to ensure consistent contracts and minimize breaking changes?
  2. 2Several teams share a common data model defined by interfaces, but each team has different runtime validation needs. How would you architect a solution that satisfies both compile‑time type safety and runtime validation without duplicating definitions?
  3. 3You need to expose a public SDK to external partners. How would you use TypeScript interfaces to define the SDK's public surface while allowing internal implementation changes, and what governance processes would you put in place?

Follow-up Questions

  • What are the differences between using an interface and a type alias for object shapes?
  • Can you describe a situation where declaration merging of interfaces caused unexpected behavior?
  • How would you decide between extending an interface versus creating a new one with overlapping fields?
Share

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