TypeScript in real projects · mid
Typing functions, overloads and this
Function types describe parameters and return values; overloads give several call signatures for one implementation; optional and rest parameters, and callback typing, catch most real bugs.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Function type:
type Handler = (e: Event) => void. Optional params(a?: string), rest(...xs: number[]). - Overloads: list signatures above one implementation signature; callers only see the overloads. Often a union or generic is simpler.
- A callback returning a value where
voidis expected is allowed (soarr.forEach(x => list.push(x))works). thiscan be typed as a first fake parameter:function f(this: HTMLElement) {}.
The red pen
Common traps: mistake → fix
Mistake: "overloads are checked against each other"
Fix: the implementation must be compatible, but callers can't call it directly.
Easy to hard
Questions interviewers ask, easy to hard
- How do you type a callback prop?
- When are overloads better than a union parameter?
- Why can a function returning number be passed where void is expected?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.