Skip to content
tezvyn:
Menu

How do you derive a PATCH payload type from UserProfile?

Source: typescriptlang.orgMediumHow cards are made

Tests knowledge of TypeScript utility types for APIs. A great answer uses Partial<UserProfile> to make all fields optional in one line and connects it to PATCH subset semantics. Red flag: manually rewriting properties as optional or confusing PUT with PATCH.

What's really being asked

This question evaluates whether you can use TypeScript's standard library to model HTTP PATCH semantics at the type level. The interviewer cares if you know that utility types like Partial are mapped types under the hood and whether you can connect language features to API contract design. It also checks if you understand the difference between partial updates and full resource replacement.

The full answer

First, state that the correct type is Partial of UserProfile. Second, explain that Partial constructs a mapped type which iterates over every key in UserProfile and makes each property optional while preserving the original value types. Third, mention that this matches RFC 5789 PATCH semantics where the client sends only the fields that need to change. Fourth, note that this approach avoids duplication and stays in sync if UserProfile later gains new fields. Fifth, optionally add that if certain fields should never be patched, such as an id or createdAt timestamp, you could combine Partial with Omit to exclude those keys before making the remainder optional.

The mistakes people make

A red flag is manually redeclaring every field as optional in a new interface because it duplicates the source of truth and breaks maintenance. Another red flag is suggesting Pick without explaining that Pick requires explicitly naming keys, which does not scale to arbitrary subsets. Some candidates propose Required or Readonly, which move in the wrong direction by making fields mandatory or immutable. Confusing PATCH with PUT is also a signal of weak API design intuition; PUT implies sending the entire resource, so Partial would be inappropriate there.

What usually comes next

The interviewer might ask how to prevent patching sensitive fields like id or emailVerified. They might also ask how to write a generic PatchPayload of T helper that works for any entity. Another follow-up is how to enforce that at least one field is present rather than allowing an empty object, which leads into discussions of Partial combined with Record and intersection types or custom mapped types with constraints.

A concrete example

Imagine UserProfile has id as string, name as string, age as number, and isAdmin as boolean. The derived type is Partial of UserProfile, so a valid payload can be an empty object, an object with only name, or an object with age and isAdmin together. If the business rule says id must never change, the safer type is Partial of Omit of UserProfile and id. This keeps the patch payload flexible for all other fields while locking the immutable key.

Interview question

When deriving a PATCH payload type from UserProfile, which approach preserves maintainability and matches partial-update semantics?

  • a.Apply Required to UserProfile so the client must send the complete resource
  • b.Manually redeclare an interface with every UserProfile field marked optional
  • c.Apply Pick to UserProfile naming only the keys allowed in the patch
  • d.Apply Partial to UserProfile making all properties optional while keeping value typesCorrect
Why?

Partial constructs a mapped type that makes each UserProfile property optional, aligning with RFC 5789 PATCH semantics and avoiding duplication. Manually redeclaring optional fields is tempting but creates a duplicate source of truth that falls out of sync.

Just read this? Test yourself on what you have been reading.

Read the original → typescriptlang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on typescript — each one lists the topics its interview covers.

See open roles