Question presented to candidate:
"TypeScript ships utility types like Partial, Pick and Omit. What do they do, how are they built, and when would you reach for each one in real code?"
What a strong answer should cover:
- They are generic types in the standard library that derive a new object type from an existing one, so one source type stays the single source of truth
Partial<T>makes every property optional,Required<T>makes every property mandatory,Readonly<T>makes every property readonly,Pick<T, K>keeps keysK,Omit<T, K>drops keysK- They are mapped types over
keyof T(OmitisPickplusExclude), and they are shallow: nested objects are not changed - Typical uses: update patches (
Partial<Omit<User, "id">>), create inputs (Omit<User, "id">), previews (Pick), and combining them (make only some keys optional) - Traps:
Omitdoes not check its keys,Omiton a union collapses it to the shared keys, andPartiallets a spread overwrite a value withundefined
Clarifying questions expected:
- Should the derived type change if the source type changes (derive it), or is it a separate contract (write it out)?
- Is
exactOptionalPropertyTypeson? It changes what an optional property may hold
Code / implementation expected: Optional β derive a patch and a create type from one interface, or write Partial by hand as a mapped type.