TypeScript Interview Questions: 14 Answers With Code
Fourteen TypeScript interview questions with answers and code: types vs interfaces, unknown vs any, generics, narrowing, utility and mapped types.
TypeScript is now standard for most React, Angular, Next.js and Node.js codebases, so interviews for frontend and full stack roles almost always include TypeScript questions. Interviewers want to see that you use the type system to prevent bugs, not just add annotations to satisfy the compiler. Here are fourteen questions that come up often, with concise answers and code you can explain.
1. Why use TypeScript over JavaScript?
It adds static types that catch mistakes at compile time — misspelled properties, wrong argument types, unhandled null values — and makes large codebases easier to refactor and navigate with editor support. It compiles to plain JavaScript, so runtime behaviour does not change. Mention that types disappear at runtime, which is why you still validate external data.
2. type vs interface
Both describe object shapes. Interfaces can be extended and merged (declaration merging), which suits public APIs and object-oriented patterns. Type aliases can express unions, intersections, tuples and mapped types.
A reasonable rule: use interfaces for object shapes you expect to extend, types for unions and compositions. Consistency within a codebase matters more than the choice.
3. any vs unknown vs never
`any` disables type checking; `unknown` is the safe alternative that forces narrowing; `never` represents values that cannot occur, such as a function that always throws or an exhausted union.
4. What is type narrowing?
Narrowing refines a broad type to a specific one using checks the compiler understands: `typeof`, `instanceof`, `in`, equality checks, and discriminant properties.
5. What is a discriminated union and exhaustive checking?
A union of object types sharing a literal property (the discriminant). Combine it with `never` to make the compiler flag unhandled cases.
If someone adds a new event type, this function fails to compile until it is handled.
6. Explain generics with an example
Generics let functions and types work with many types while preserving the relationship between input and output.
7. What are generic constraints?
`K extends keyof T` restricts keys to those that exist on `T`.
8. Name some utility types and when you use them
- `Partial<T>`: all properties optional — useful for update payloads.
- `Required<T>`: all properties required.
- `Pick<T, K>` and `Omit<T, K>`: select or remove properties — useful for API response shapes.
- `Readonly<T>`: prevent reassignment of properties.
- `Record<K, V>`: map keys to values, such as `Record<Status, string>` for labels.
- `ReturnType<F>` and `Parameters<F>`: derive types from functions.
9. What are mapped types?
Utility types like `Partial` are built this way.
10. What are conditional types?
`infer` extracts a type from within another type. Keep answers practical; deep type-level programming is rarely needed in application code.
11. What does strict mode change?
`"strict": true` enables several checks, notably `strictNullChecks` (null and undefined must be handled explicitly) and `noImplicitAny`. Most teams enable it; it catches a large class of runtime errors.
12. How do you type React components?
Also be ready to type `useState` with unions, event handlers (`React.ChangeEvent<HTMLInputElement>`), and generic components.
13. How do you handle data from APIs safely?
Types do not exist at runtime, so a response typed as `User` may not be one. Validate external data at the boundary with a schema library such as Zod, and infer the TypeScript type from the schema to keep them in sync.
14. What is the difference between `as` and type annotations?
An annotation (`const u: User = …`) asks the compiler to check the value matches. An assertion (`value as User`) tells the compiler to trust you, which can hide bugs. Prefer annotations and narrowing; use assertions sparingly and with a comment explaining why.
Interviewers often ask, "Tell me about a bug TypeScript caught for you." Have a real example ready — a renamed API field, a missing null check, an unhandled union case — it shows practical experience better than definitions.
Practice tasks
- Type a `useFetch` hook that returns loading, error and data states as a discriminated union.
- Write a type-safe `groupBy` with generics.
- Implement your own `Partial` and `Pick` with mapped types.
- Convert a JavaScript module with `any` everywhere into strict TypeScript.
Getting to the interview
Make sure your resume names TypeScript and your frameworks in context; the Algnetic ATS resume builder can check it against target postings, and auto-apply can keep applications moving while you prepare.
Key takeaways
- Know type vs interface, any vs unknown vs never, narrowing and discriminated unions.
- Be fluent with generics, constraints, utility types and mapped types.
- Understand that types vanish at runtime; validate external data.
- Prefer annotations and narrowing over `as` assertions.
- Bring a real example of TypeScript preventing a bug.
Let Algnetic apply to matching jobs for you · More career guides