TypeScript · Senior · Question 14 of 75
In what order should function overloads be written?
- ✓Correct answer: From more specific to more general - TS picks the first match from the top
- 2From general to specific - TS picks the last match
- 3Order doesn't matter: TS picks the most precise signature itself
Explanation
If a general overload (x: any) comes first, it "captures" every call and the specific signatures become useless. Another tip: don't write overloads that differ only in the last parameter - use an optional parameter instead.