Question presented to candidate: "Can a generic type parameter have a default? How does that interact with inference and with constraints, and where have you seen defaults used?"
What a strong answer should cover:
- Yes:
interface Box<T = string>β writingBoxalone meansBox<string>; defaults arrived in TypeScript 2.3 - Rules: a parameter with a default is optional, required parameters cannot follow optional ones (TS2706), a default must satisfy its constraint (TS2344), and a default may refer to earlier parameters (
<T, C = T[]>) - For function calls, inference wins: the default only applies when there is nothing to infer from (
box()vsbox(10)); with neither,Tbecomesunknown - No partial inference: pass one type argument explicitly and the rest come from their defaults, not from the arguments
- Good defaults:
unknownfor parse/fetch helpers, a loose event map for an emitter, React typings such asuseState<S = undefined>()
Code / implementation expected: Optional β a generic interface or function with a default, and one call where inference overrides it.