TypeScript Middle interview questions and answers
75 questions for a TypeScript (Middle) interview - with correct answers, explanations and links to the docs.
Take this quiz- 1.What is a user-defined type guard?
- 2.How do you guarantee that a
switchhandled every member of a union? - 3.What does
<T extends { id: string }>mean in a generic? - 4.How do you type
getProp(obj, key)so the result depends on the key? - 5.What is a mapped type?
- 6.What does the conditional type
T extends string ? 'text' : 'other'do? - 7.What does
inferdo intype ElementType<T> = T extends (infer U)[] ? U : never? - 8.What does
Awaited<Promise<Promise<number>>>return? - 9.How does
Exclude<T, U>differ fromOmit<T, K>? - 10.How do function overloads work in TypeScript?
- 11.What is declaration merging?
- 12.What is an abstract class for?
- 13.How does
protecteddiffer fromprivatein TypeScript classes? - 14.What does the
satisfiesoperator do? - 15.What are template literal types?
- 16.Why is an
as constobject often used instead of anenum? - 17.How does
const enumdiffer from a regularenum? - 18.How do namespaces (
namespace) differ from ES modules? - 19.What is an assertion function?
- 20.How do you get a native button's props to extend them in your component?
- 21.How do you type Redux Toolkit hooks in a TypeScript app?
- 22.How do you type a generic list component?
- 23.What does
Extract<'a' | 'b' | 1 | 2, string>produce? - 24.What does
NonNullable<string | null | undefined>return? - 25.How do
object,{}andRecord<string, unknown>differ as types? - 26.How does TypeScript handle the
inoperator when narrowing? - 27.What do
-readonlyand-?do in a mapped type? - 28.How do you rename keys in a mapped type?
- 29.Why does
Object.keys(user)returnstring[]rather than(keyof User)[]? - 30.What does the
noUncheckedIndexedAccessflag give you? - 31.How does
unknown[]differ fromany[]in a function parameter? - 32.What are decorators in TypeScript 5?
- 33.What does
import type { User } from './types'do? - 34.Why is
interfacepreferable to an intersection for extending large object types? - 35.What does
Parameters<(a: string, b?: number) => void>return? - 36.How do you specify the type of
thisfor a handler function? - 37.What's the best way to type an API response whose structure differs by status?
- 38.What does the
ReadonlyArray<T>utility (orreadonly T[]) do? - 39.What is
unique symbol? - 40.How do you extend the type of the global
Windowobject from a module? - 41.How do you type the result of
JSON.parsesafely? - 42.How does
type A = Bdiffer frominterface A extends B {}in IDE tooltips? - 43.What does
pathsintsconfig.jsongive you? - 44.How does
moduleResolution: 'bundler'differ from'node16'/'nodenext'? - 45.What does the
isolatedModulesflag do? - 46.What does
keyof typeof objdo? - 47.When does TS widen a literal type to a general one?
- 48.How do you make a generic parameter optional with a default value?
- 49.How do you type a factory function that returns the same type it received?
- 50.What is contextual typing?
- 51.Why does
Omit<Circle | Square, 'id'>lose the discriminated union? - 52.What does
as const satisfies Record<string, Route>give you for a config object? - 53.When should you prefer a union parameter over overloads?
- 54.What are parameter properties, and why are they not "erasable" syntax?
- 55.What do the
overridekeyword andnoImplicitOverrideprotect against? - 56.Why might
Readonly<Settings>not prevent this change? - 57.How do you derive a TypeScript type from a Zod schema?
- 58.What does type-aware linting with typescript-eslint add on top of
tsc? - 59.How do you share one strict base configuration between many packages?
- 60.How do
targetandlibdiffer intsconfig.json? - 61.What does
esModuleInteropfix? - 62.Which option lets you
import config from './config.json'with typed contents? - 63.How do you get the instance type and constructor arguments of a class?
- 64.How do you narrow the results of
Promise.allSettled? - 65.What do the intrinsic types
Uppercase,Capitalizeetc. allow? - 66.What do
noImplicitReturnsandnoFallthroughCasesInSwitchcatch? - 67.How do you type a generator function?
- 68.How do you type a generic data-fetching hook?
- 69.When is
Map<K, V>a better choice than an index signature object? - 70.What do the
types/typesVersionsand theexports"types"condition inpackage.jsondo? - 71.What does
export type * from './types'do (TS 5.0+)? - 72.How do you write a type for any JSON value?
- 73.How do you implement a type-safe event emitter?
- 74.Does narrowing work when a discriminated union is destructured first?
- 75.What does
switch (true)narrowing (TS 5.3) enable?