TypeScript · Middle · Question 53 of 75
When should you prefer a union parameter over overloads?
function len(x: string | any[]): number { return x.length; }- ✓Correct answer: When the return type doesn't depend on which input type was passed
- 2Never - overloads are always clearer
- 3Only for class methods
Explanation
Overloads can't be called with a value of the union type unless an overload accepts the union. Use them only when inputs map to different outputs.