Advertisement
Intermediate TypeScript, Structural Typing & Duck Typing

How does structural typing handle a function type that has FEWER required parameters than expected, versus one with MORE?

Short answer

A function with FEWER declared parameters is structurally compatible where MORE are expected (since it simply ignores extra arguments passed to it, which is safe), but a function requiring MORE parameters than the target type provides is NOT compatible, since it would be called…

Save TypeScript mock interview
Advertisement
AModel answer
A function with FEWER declared parameters is structurally compatible where MORE are expected (since it simply ignores extra arguments passed to it, which is safe), but a function requiring MORE parameters than the target type provides is NOT compatible, since it would be called without enough arguments — matching how real function calls behave at runtime.
type Callback = (a: number, b: number) => void;
const shortFn: Callback = (a) => console.log(a); // OK -- fewer params is fine
// const longFn: Callback = (a, b, c) => console.log(a, b, c); // Error: too many required params
Real-world example
Passing a simplified callback like `(item) => ...` where an array method's callback type technically expects `(item, index, array) => ...`.
Likely follow-ups
  • Why is this particular asymmetry considered safe from a pure function-call perspective?
Related topics