Interview Q&A · questions with model answers
TypeScript Developer Interview Questions
Part of the study plan for: Frontend Engineer · Full Stack Engineer
Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.
-
How do you type React component props, children and events in TypeScript?
Declare a props type or interface and annotate the function parameter; type children as React.ReactNode; use event types like React.ChangeEvent<HTMLInputElement> and…
-
How do you type hooks like `useState`, `useReducer`, `useRef` and context?
useState infers from the initial value; pass a generic when the initial value is narrower than the eventual type (useState<User | null>(null)). Type useReducer with a discriminated union of actions.…
-
How do you write a generic React component (e.g., a typed List or Select)?
Declare the component as a generic function, function List<T>({ items, renderItem }: ListProps<T>); TypeScript infers T from the props at the call site, so callbacks receive correctly typed items.
-
How would you migrate a large JavaScript codebase to TypeScript?
Incrementally: add tsconfig with allowJs (and optionally checkJs with JSDoc types), rename files to .ts gradually starting from leaf modules, add types for shared models first, start with loose…
-
How do you model immutability in TypeScript?
Use readonly properties, Readonly<T>, ReadonlyArray<T>/readonly T[], as const, and ReadonlyMap/ReadonlySet. These prevent mutation at compile time, but don't freeze objects at runtime.
-
How should you handle errors in `catch` blocks in TypeScript?
With useUnknownInCatchVariables (part of strict), the caught value is unknown, so narrow it (if (err instanceof Error)) before using .message. For typed error handling, consider result types ({ ok:…
-
How do you test types themselves?
Write type-level assertions with tools like Vitest's expectTypeOf, tsd, or @ts-expect-error comments in test files, and run tsc in CI so regressions in complex generic types are caught.
-
How do you type an Express/Node API in TypeScript?
Use @types/express (or Express 5's types), type route handlers with Request<Params, ResBody, ReqBody, Query>, validate request bodies at runtime with Zod and infer the types, augment Request for…
-
How can you share types between frontend and backend?
Options: a shared package of DTO types/schemas in a monorepo, generating types from an OpenAPI schema (openapi-typescript), GraphQL codegen, or end-to-end typed RPC like tRPC, where the client…
-
How would you implement a `DeepPartial<T>` type?
Use a recursive mapped type that makes each property optional and recurses into objects, while leaving functions and primitives alone.
-
How do you type async functions and promises?
An async function's return type is Promise<T>. Use Awaited<T> to unwrap promise types, type Promise.all results via inference (tuples are preserved), and use PromiseSettledResult<T> for allSettled.
-
How do you type environment variables safely?
Validate process.env at startup with a schema (Zod, envalid, @t3-oss/env-*) and export a typed config object, rather than augmenting NodeJS.ProcessEnv and assuming values exist.
-
How do you publish a TypeScript library with correct types for ESM and CommonJS consumers?
Emit JavaScript plus .d.ts files, set package.json exports with types/import/require conditions (and .d.mts/.d.cts where needed), set type, and verify with tools like @arethetypeswrong/cli and…
-
How would you type an event emitter so event names and payloads are checked?
Define an event map type ({ login: { userId: string }; logout: void }) and make on/emit generic over K extends keyof Events, so the listener receives Events[K].
-
How do you write a type-safe fetch wrapper?
Make the wrapper generic over the response type but validate the JSON at runtime with a schema passed in, e.g. getJson<T>(url, schema: z.ZodType<T>): Promise<T>, and return a discriminated result or…
-
How do you implement mixins in TypeScript?
Write functions that take a base class constructor (TBase extends new (...args: any[]) => {}) and return a class extending it with added members; compose them like Timestamped(Activatable(User)).
-
How do you add types to globals like `window`?
In a module file, use declare global { interface Window { analytics: Analytics } } and include export {} so the file is a module; interface merging adds the property to the global Window type.
-
How do you type Maps, Sets and WeakMaps?
Use generics: new Map<string, User>(), new Set<number>(), new WeakMap<object, Metadata>(). map.get(key) returns V | undefined, so you must handle missing values.
-
How would you type a generic repository class?
Parameterize the class over the entity type and ID type (class Repository<T extends { id: Id }, Id = string>), with methods returning Promise<T | undefined>, and use Omit<T, 'id'> for create inputs.
-
How do you safely use data from `JSON.parse` without a schema library?
Treat the result as unknown and write type guards that check each required field's type, returning value is T, before using it.
-
How do you make a class implement an interface, and what does `implements` actually check?
class Foo implements Bar makes the compiler check that Foo's instance side satisfies Bar; it doesn't change the class's type or add members, and the class could satisfy the interface structurally…