TypeScript interview questions
BetaThe type system beyond annotations: narrowing, generics, utility types, and how to model real domains.
Subtopics
- Type annotations & inference7
- Interfaces vs type aliases6
- Union, intersection & literal types7
- Narrowing & type guards8
- Generics & constraints8
- Utility types7
- Mapped & conditional types6
- Enums & const assertions6
- unknown, any, never & strictness6
- Declaration files & module resolution6
- Classes, access modifiers & abstract7
- Async types & error handling6
Sample questions
- CodingJuniorAsync types & error handling
Write
async function safeDivide(a: number, b: number): Promise<{ ok: true; value: number } | { ok: false; error: string }>that resolves to{ ok: true, value: a / b }, or, whenbis0, resolves to{ ok: false, error: "Division by zero" }instead of throwing. - TheoryJuniorAsync types & error handling
What does the return type of an
asyncfunction always look like? Why can't an async function just returnTdirectly? - CodingJuniorClasses, access modifiers & abstract
Write a
BankAccountclass with a privatebalancestarting at 0,deposit(amount: number): void, andwithdraw(amount: number): booleanthat subtractsamountand returnstrueonly if the balance is sufficient (otherwise it changes nothing and returnsfalse). Then - CodingJuniorClasses, access modifiers & abstract
Write an abstract class
Shapewithabstract area(): number, and concrete subclassesCircle(constructed with a radius) andRectangle(constructed with width and height). Then writetotalArea(shapes: ({ kind: "circle"; radius: number } | { kind: "rectangle"; width: number; height: number })[]): number - TheoryJuniorClasses, access modifiers & abstract
What do
public,privateandprotectedmean on a class member? Are they enforced at runtime? - CodingJuniorDeclaration files & module resolution
Write
declarationFileNameFor(sourceFile: string): stringthat returns the declaration-file name for a JS source file: if it already ends with.d.ts, return it unchanged; if it ends with.js,.jsx,.mjsor.cjs, replace that extension with.d.ts; otherwise, append.d.ts