tezvyn:

How would you combine BaseRecord and PostContent into a Post type?

AI-drafted, machine-checkedSource: typescriptlang.orgintermediate

This tests TypeScript type composition for API resources. A strong answer proposes an intersection type or interface extends, then discusses overlap conflicts and the type versus interface trade-off.

WHAT THIS TESTS: This question probes your ability to model API resources using TypeScript static type composition rather than runtime object manipulation. The interviewer wants to see that you understand the difference between compile-time type merging and runtime value merging, and that you can choose between type aliases and interfaces appropriately when building layered domain models.

A GOOD ANSWER COVERS: First, present the two canonical approaches. You can define a type alias using the intersection operator, like type Post = BaseRecord & PostContent. Alternatively, you can define an interface that extends both sources, like interface Post extends BaseRecord, PostContent. Second, explain the structural behavior of intersections: if both input types declare the same property with incompatible types, the resulting property becomes never, which surfaces a compile error that forces you to reconcile the conflict. Third, discuss when to prefer each approach. Interface extends is generally favored for object-oriented shapes that may need declaration merging across modules, while intersection types are more flexible for composing unions, mapped types, or utility type chains. Fourth, mention that both approaches produce a static type only; they do not perform runtime object copying.

COMMON WRONG ANSWERS: Suggesting Object.assign or the spread operator as the primary solution is a red flag because those are runtime value operations, not type definitions. Another weak pattern is manually redeclaring every property from BaseRecord and PostContent into Post, which defeats the purpose of type reuse and signals unfamiliarity with composition. Some candidates also claim that interface extends cannot handle overlapping properties, which is incorrect; it behaves similarly to an intersection but with clearer error messages.

LIKELY FOLLOW-UPS: The interviewer may ask what happens if BaseRecord and PostContent both declare a title property with different types, such as string versus number. They may also ask you to implement a generic WithBaseRecord helper that takes any content type and produces the full resource shape. A third follow-up could involve converting the final Post type into a partial update DTO by mapping its properties to optional or omitting the id and timestamps.

ONE CONCRETE EXAMPLE: Imagine BaseRecord has id as string, createdAt as Date, and updatedAt as Date, while PostContent has title as string and body as string. Using type Post = BaseRecord & PostContent gives you a type that requires all five fields. If you later add an authorId to BaseRecord, every resource using the intersection automatically inherits it without touching PostContent or Post. If you instead needed a create payload that omits the timestamps, you could derive CreatePostDto by using Omit on the intersection or by intersecting PostContent with Pick of BaseRecord.

Read the original → typescriptlang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.