TypeScript interview questions and answers for developers
TypeScript interview questions check whether you can model data with types that catch real bugs: unions and narrowing, generics with constraints, utility and mapped types, and the strict compiler options. Interviewers also ask what TypeScript cannot do, mainly that types are erased at runtime, so data from an API must still be validated. Expect concept questions, "what type is this" puzzles and a task to type an existing function or write a small utility type.
By Evin JohnPublished 7 min read
Where TypeScript questions appear
Most frontend, full stack and Node.js job descriptions now list TypeScript, so a TypeScript developer interview is usually a JavaScript or React round with typed code rather than a separate quiz. You may be asked to add types to an untyped function, fix compiler errors in a pull request, design the types for an API response, or explain a type you wrote. Senior candidates get questions about strictness settings and how they introduced or tightened TypeScript in a codebase.
- Type system basicsShort questions on inference, unions, interfaces and the special types. Precise vocabulary helps: narrowing, widening, assignability.
- Type the codeA plain JavaScript function or component to type well, without reaching for any.
- Type puzzlesPredict an inferred type or write a utility type such as your own Pick or a DeepReadonly.
- Codebase judgmentHow strict to be, how to type third-party data, and how to migrate a JavaScript project file by file.
TypeScript fundamentals questions
| Question | What it tests | What a strong answer shows |
|---|---|---|
| What does structural typing mean? | Type compatibility | Types are compatible when their shapes match, not their names. Any object with the right properties satisfies an interface, even if it was never declared to implement it. |
| type alias or interface? | Declaring shapes | Both describe objects. Interfaces can be merged by redeclaring and extend cleanly; type aliases also cover unions, tuples, mapped and conditional types. Pick one convention for object shapes and keep it. |
| any, unknown and never: what is the difference? | Special types | any turns checking off in both directions. unknown accepts anything but must be narrowed before use. never has no values: the return type of a function that always throws, or the leftover of an exhausted union. |
| What happens to types at runtime? | Type erasure | They are removed when compiling. Nothing checks JSON from a server, so validate it with a schema library or hand-written guards at the boundary. |
Why is let a = "hi" a string but const b = "hi" the type "hi"? | Literal widening | A const cannot change, so it keeps the literal type. A let widens to string. as const keeps literals and makes arrays readonly tuples. |
| Why does passing an object literal with an extra property fail, but passing a variable with it succeeds? | Excess property checks | Fresh object literals get an extra check for unknown keys, because an extra key there is probably a typo. A variable is judged only by structure. |
| Enum or a union of string literals? | Modeling constants | Unions are erased entirely and work well with narrowing. Enums emit runtime objects; const enums are awkward with isolated per-file compilers. Many teams prefer unions or an as const object. |
| What does strictNullChecks change? | Null safety | null and undefined stop being assignable to every type, so you must handle them. It is the single most valuable strict option. |
What does the ! non-null assertion do? | Escape hatches | Tells the compiler a value is not null without checking. Acceptable after a check the compiler cannot follow; a smell when used to silence errors. |
| readonly versus const? | Immutability | const stops rebinding a variable. readonly stops writing a property or array through that type. Neither freezes the object at runtime. |
let greeting = "hello"; // string
const fixed = "hello"; // "hello"
const pair = [1, "a"]; // (string | number)[]
const tuple = [1, "a"] as const; // readonly [1, "a"]
interface Point { x: number; y: number }
function draw(p: Point) {}
const p3 = { x: 1, y: 2, z: 3 };
draw(p3); // OK: structural match
draw({ x: 1, y: 2, z: 3 }); // Error: excess property 'z'Narrowing and union questions
Narrowing is where TypeScript earns its keep, and interviewers often ask you to model a state machine or an API result as a discriminated union. The pattern below is worth being able to write from memory, including the exhaustive check.
type Shape =
| { kind: "circle"; radius: number }
| { kind: "square"; side: number }
| { kind: "rect"; width: number; height: number };
function area(s: Shape): number {
switch (s.kind) {
case "circle":
return Math.PI * s.radius ** 2;
case "square":
return s.side ** 2;
case "rect":
return s.width * s.height;
default: {
const unreachable: never = s;
return unreachable;
}
}
}- Which operators narrow a union? typeof for primitives, instanceof for classes,
infor property checks, equality against literals, truthiness checks, and Array.isArray. - What is a user-defined type guard? A function returning
value is T, such asisString(x: unknown): x is string. The compiler trusts it, so a wrong guard is a silent bug. - What is an assertion function? A function declared with
asserts value is Tthat throws when the condition fails, narrowing the value for the rest of the scope. - How would you type a loading, success and error request state? A union with a
statusfield, where data exists only on success and error only on failure, so the UI cannot read data while loading. - Why might narrowing be lost inside a callback? The compiler cannot be sure a mutable variable was not reassigned before the callback runs. Copy it into a const first.
Generics and type-level questions
function getProp<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const user = { id: 1, name: "Ada" };
const name = getProp(user, "name"); // string
getProp(user, "email"); // Error: not a key of user| Question | What it tests | What a strong answer shows |
|---|---|---|
Why add a constraint such as T extends { id: string }? | Generic constraints | So the function body can use id while still returning the caller's exact type instead of a widened one. |
| Name the utility types you use most and what each does. | Standard library | Partial, Required, Readonly, Pick, Omit, Record, ReturnType, Parameters, Awaited and NonNullable, each with a real use such as Partial for an update payload. |
| Write your own Pick. | Mapped types | type MyPick<T, K extends keyof T> = { [P in K]: T[P] }. Explain the constraint on K. |
What does infer do? | Conditional types | Declares a type variable to capture inside a conditional, such as the element type of an array or the resolved type of a promise. |
| What is a distributive conditional type? | Unions in conditionals | A conditional on a naked type parameter is applied to each union member separately. Wrapping both sides in a tuple turns that off. |
| What does the satisfies operator give you over an annotation? | Checking without widening | It checks a value against a type but keeps the more specific inferred type, so later code still knows each property's exact shape. |
| How do function overloads work? | Signatures | Several call signatures over one implementation. Callers see only the overloads; the implementation signature must be compatible with all of them. |
| What is a template literal type? | String types | A string type built from a pattern. Combined with the built-in Capitalize type, it can turn an event name such as "click" into a handler key such as "onClick" for every event in a union. |
type ToArray<T> = T extends unknown ? T[] : never;
type A = ToArray<string | number>; // string[] | number[]
type ToArrayOnce<T> = [T] extends [unknown] ? T[] : never;
type B = ToArrayOnce<string | number>; // (string | number)[]
type ElementOf<T> = T extends readonly (infer E)[] ? E : never;
type C = ElementOf<number[]>; // number
type Theme = Record<"primary" | "accent", string | [number, number, number]>;
const palette = {
primary: "#6b5bd2",
accent: [120, 110, 210],
} satisfies Theme;
palette.primary.toUpperCase(); // OK: still known to be a stringSenior TypeScript and scenario questions
- Which tsconfig options would you turn on in a new project?
strict, plusnoUncheckedIndexedAccessso array and record lookups include undefined, andnoImplicitOverrideorexactOptionalPropertyTypeswhere the team agrees. Explain the cost of each in noise. - How do you type data from an API you do not control? Treat it as unknown, parse it with a schema that also produces the static type, and fail loudly at the boundary rather than deep in the UI.
- How would you migrate a large JavaScript codebase? Turn on
allowJsandcheckJsgradually, convert leaf modules first, type the shared boundaries early, ban new any with a lint rule, and raise strictness one flag at a time. - What goes in a .d.ts file? Declarations without implementations: types for a JavaScript library, global augmentations, or module declarations for assets. Community types for packages often come from
@typespackages. - A build is slow and the editor lags. What do you check? Huge unions, deeply recursive types, and project size. Project references and incremental builds help, and
--extendedDiagnosticsshows where time goes. - When is the any type acceptable? At a boundary you are about to narrow, in tests, or as a time-boxed step in a migration. Prefer unknown, and leave a comment when you do use any.
- Why does TypeScript accept an array of Dogs where an array of Animals is expected, even though that is not fully safe? Arrays are treated as covariant for practicality. Writing a Cat into that array would type-check. Mention readonly arrays as the safer parameter type.
- How do you share types between a Node.js API and a React client? A shared package or a schema-first approach (OpenAPI or a validation library) that generates or exports the types used on both sides.
| Scenario | Likely cause | What a strong answer shows |
|---|---|---|
| Property does not exist on type never. | Over-narrowing | Every union member was ruled out, often by a check that is always false or a mistyped literal. Inspect the type before the check. |
| Type string is not assignable to type "GET" | "POST". | Widening | The value was inferred as string. Use as const, annotate the variable, or pass the literal directly. |
| Object is possibly undefined after a find(). | Real missing case | find can return undefined. Handle the not-found branch instead of adding !. |
| The types compile but production crashes on a missing field. | Erasure | The response did not match the declared type. Validate at runtime and add a test with the real payload. |
Fictional example
- We had a mid-sized React app with strict off and a few hundred uses of any. Turning strict on at once produced thousands of errors, so I enabled strictNullChecks alone first, in a separate config that CI ran as a non-blocking report. Each team fixed the files they touched in normal work, and we added a lint rule that blocked new any. Once the report reached zero for a folder, that folder moved into the blocking config. It took about two months. The part I would repeat is validating API responses at the fetch layer early on, because most of the real null bugs were in server data that our types had simply assumed.
TypeScript practice plan
- Days 1 to 3: inference and narrowing. Type five untyped functions from a past project without any, and model one request state as a discriminated union.
- Days 4 to 6: generics. Write getProp, a typed groupBy, and a typed event emitter whose handler parameters follow the event name.
- Days 7 to 9: type-level work. Recreate Pick, Omit, Partial, ReturnType and Awaited, then a DeepReadonly, explaining each line aloud.
- Days 10 and 11: configuration and boundaries. Read your own tsconfig line by line and add runtime validation to one API call.
TypeScript builds directly on JavaScript interview questions, and typed components come up in React interview questions. Server-side typing questions often sit inside Node.js interview questions.
Type puzzles are easy to read and hard to explain under time pressure. When you run a mock round with Natively, an AI interview assistant on Mac and Windows, it records a transcript of what you said about each type, and it can read a compiler error or a typing task from a screenshot in practice and propose an approach. Pro or the free trial lets it work from the résumé and job description you add. The transcript and any notes remain in your history afterwards.
Questions people ask
What TypeScript questions are asked in interviews?
Type versus interface, any versus unknown versus never, narrowing and discriminated unions, generics with keyof constraints, utility types such as Partial and Omit, and what happens to types at runtime.
How hard are TypeScript developer interviews?
Most stay practical: type an existing function well, fix compiler errors or model an API response. Senior roles add conditional and mapped types, tsconfig choices and migration strategy.
What is the difference between unknown and any?
Both accept any value. any turns type checking off, while unknown forces you to narrow the value with a check before you use it, so it is the safe choice for untrusted data.
Does TypeScript validate data at runtime?
No. Types are erased when the code compiles, so data from APIs, files or users must be validated at runtime with guards or a schema library.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
