TypeScript · Middle · Question 53 of 75

When should you prefer a union parameter over overloads?

function len(x: string | any[]): number { return x.length; }

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.

Further reading

Problem with a question

TypeScript · Middle · question 53

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz