TypeScript Senior interview questions and answers
75 questions for a TypeScript (Senior) interview - with correct answers, explanations and links to the docs.
Take this quiz- 1.With
strictFunctionTypes, can you assign(x: Dog) => voidto a variable of type(x: Animal) => void? - 2.Why doesn't
strictFunctionTypesapply to a method declared ashandle(x: Dog): void? - 3.What is
ToArray<string | number>iftype ToArray<T> = T extends any ? T[] : never? - 4.How do you turn off a conditional type's distribution over a union?
- 5.Why does
type IsNever<T> = T extends never ? true : falsegiveneverforIsNever<never>? - 6.How do you implement
DeepPartial<T>? - 7.Why are branded types needed?
- 8.How do you keep only the function-valued properties of a type?
- 9.What does
Split<'a/b/c'>return? - 10.What are variadic tuple types?
- 11.What does the
constmodifier on a type parameter (TS 5.0) give you? - 12.What is the
NoInfer<T>utility (TS 5.4) for? - 13.What does
infer N extends numberdo in a conditional type? - 14.In what order should function overloads be written?
- 15.How do you add a
userfield to the Express library'sRequesttype? - 16.What does the
verbatimModuleSyntaxflag require? - 17.What does the
erasableSyntaxOnlyflag (TS 5.8) forbid? - 18.What are Project References (
references,composite) for? - 19.What's the risk of enabling
skipLibCheck: true? - 20.How do you find the cause of slow type checking in a large project?
- 21.Can you pass a
readonly string[]to a function that acceptsstring[]? - 22.What does a
thisreturn type give you in a class method? - 23.What are the
inandoutvariance annotations on type parameters (TS 4.7) for? - 24.Why can the template type `
${A}-${B}-${C}` over three large unions cause an error? - 25.How does the
UnionToIntersectiontrick work? - 26.Why is
Record<Status, string>more useful than{ [k: string]: string }for a dictionary keyed by status? - 27.What changed about type predicate inference in TS 5.5?
- 28.What are
usingdeclarations (TS 5.2) for? - 29.What does
(typeof users)[number]return ifusers: User[]? - 30.What does the mapped type
{ [K in keyof T]: Box<T[K]> }produce ifTis the tuple[string, number]? - 31.How do you get the element type of a
Set<T>orMap<K, V>from a value? - 32.What is the
declarekeyword for? - 33.How do you check a type in tests so that you get an error if there's NO compile error?
- 34.When is the double assertion
x as unknown as Tjustified? - 35.Why should you avoid the
Functiontype for callbacks? - 36.Why can't you write
a.xfora: { x: string } | { y: number }? - 37.What does the
exactOptionalPropertyTypesflag change? - 38.What does
keyof (A | B)return ifA = { id; name }andB = { id; price }? - 39.How do you enable type checking in JS files without migrating to TS?
- 40.How do you type a parameter that accepts an abstract class constructor?
- 41.Is the narrowing of a
letvariable preserved inside a callback? - 42.What is a homomorphic mapped type, and what does it preserve?
- 43.How do you type a
Promise.all-like function over a tuple of promises? - 44.Why is it recommended to annotate return types of exported functions in a library?
- 45.Why is
isolatedDeclarations(TS 5.5) useful for monorepos? - 46.How does
T extends objectdiffer fromT extends {}as a generic constraint? - 47.Why is extending an interface faster for the compiler than an intersection?
- 48.How do you type a reducer so that
payloaddepends on the action'stype? - 49.What does
T[keyof T]return forT = { a: string; b: number }? - 50.What is
declare module '*.svg'used for? - 51.Why does this compile, and what's the soundness hole?
- 52.What does tail-recursion elimination for conditional types (TS 4.5) allow?
- 53.How do you extract route params from a path string type?
- 54.How does a type-safe builder accumulate information in its type?
- 55.TypeScript has no higher-kinded types. How do libraries emulate
F<A>whereFis generic? - 56.What does the error "The inferred type of 'x' cannot be named without a reference to ..." mean?
- 57.Why does a dual ESM/CJS package need separate declaration files (e.g.
.d.mtsand.d.cts)? - 58.What does
rewriteRelativeImportExtensions(TS 5.7) enable? - 59.What is the main goal of TypeScript's native (Go) port?
- 60.What do the typescript-eslint
no-unsafe-*rules help with? - 61.What does an assertion function require to work as a narrowing call?
- 62.What does a
this ispredicate do in a class method? - 63.What is
{ id: string } & { id: number }? - 64.How do you get the type of a module's exports without importing its values?
- 65.Why do excess property checks miss errors in this case?
- 66.Why can a large union of string literals slow down the compiler?
- 67.Why can a
letvariable lose its narrowing inside a callback but aconstkeeps it? - 68.Why should a public library avoid
enumin its API? - 69.How do you type a function that preserves the exact keys of an object passed to it while transforming values?
- 70.How do you prevent consumers from depending on internal types of your library?
- 71.What problem can
strictNullChecks: falsein one package cause in a monorepo? - 72.Why are explicit generic constraints better than casting inside a generic function?
- 73.What does
Object.freezereturn in terms of types? - 74.How can you make a discriminated union exhaustive check also catch a missing case at the data layer?
- 75.Why is it risky to type an API client with generics alone, like
get<T>(url): Promise<T>?