RMRM Full Stack & AI Engineer · All questions · Roadmaps
Languages · interview questions

TypeScript Interview Questions

TypeScript is a statically typed superset of JavaScript developed by Microsoft. These questions cover core concepts from basic types and interfaces to advanced generics, decorators, and compiler configuration — commonly asked in frontend, backend, and full-stack engineering interviews.

1. What is TypeScript and how does it differ from JavaScript?

beginner

TypeScript is a statically typed superset of JavaScript that compiles down to plain JavaScript. It adds optional static typing, interfaces, generics, and modern language features, enabling better tooling, earlier error detection, and improved code maintainability compared to vanilla JavaScript.

2. What are the basic primitive types in TypeScript?

beginner

TypeScript's basic primitive types are string, number, boolean, null, undefined, symbol, and bigint. These mirror JavaScript primitives but allow the compiler to enforce correct usage at compile time.

3. What is the difference between 'interface' and 'type' in TypeScript?

beginner

Both can describe object shapes, but interfaces support declaration merging (multiple declarations are merged) and are generally preferred for object/class contracts. Type aliases are more flexible — they can represent unions, intersections, tuples, and primitives — but cannot be merged after declaration.

4. What is 'any' and why should it be avoided?

beginner

The any type disables all type checking for a variable, effectively opting it out of TypeScript's type system. It should be avoided because it defeats the purpose of using TypeScript and can hide runtime bugs; prefer unknown for unknown values as it still enforces type checks before use.

5. What is the difference between 'unknown' and 'any'?

beginner

Both can hold any value, but unknown is type-safe: you must narrow or assert the type before performing operations on it. any bypasses all type checks entirely, while unknown forces explicit handling, making it the safer alternative when the type is truly not known.

6. What are union and intersection types?

beginner

A union type (A | B) means a value can be one of several types, while an intersection type (A & B) means a value must satisfy all combined types simultaneously. Union types are useful for flexible parameters; intersection types are used to merge multiple type shapes together.

7. What is type narrowing and what techniques are used?

intermediate

Type narrowing is the process of refining a broad type to a more specific one within a code block. Common techniques include typeof guards, instanceof checks, equality narrowing, the in operator, and user-defined type guards (predicates with the 'is' keyword).

8. What are generics and why are they useful?

intermediate

Generics allow you to write reusable, type-safe code that works with multiple types without sacrificing type information. For example, function identity<T>(arg: T): T returns the same type it receives, enabling strongly typed reuse across many data types.

9. What is the 'readonly' modifier and when would you use it?

intermediate

readonly prevents a property from being reassigned after initialization, similar to const but for object properties. It is useful for immutable data structures, value objects, and preventing accidental mutation in function parameters.

10. What are TypeScript decorators and what are they used for?

intermediate

Decorators are special declarations (prefixed with @) that can be attached to classes, methods, properties, or parameters to add metadata or modify behavior at design time. They are widely used in frameworks like Angular and NestJS for dependency injection, routing, and validation.

11. What is the difference between 'interface' declaration merging and extending?

intermediate

Declaration merging automatically combines multiple interface declarations with the same name into one, useful for augmenting third-party types. Extending (interface B extends A) creates a new interface that inherits members from another, which is explicit and does not affect the original interface.

12. What are mapped types and give an example?

intermediate

Mapped types create new types by transforming each property of an existing type. For example, type Readonly<T> = { readonly [K in keyof T]: T[K] } makes every property of T readonly. Built-in examples include Partial<T>, Required<T>, Record<K,V>, and Pick<T,K>.

13. What are conditional types and how do they work?

advanced

Conditional types use the syntax T extends U ? X : Y to select a type based on a condition, similar to ternary operators. They are powerful for type-level logic, for example: type NonNullable<T> = T extends null | undefined ? never : T removes null and undefined from a type.

14. What is the 'infer' keyword and where is it used?

advanced

The infer keyword is used inside conditional types to declare a type variable that TypeScript infers from the matched type. For example, type ReturnType<T> = T extends (...args: any[]) => infer R ? R : never extracts the return type of a function type.

15. What is the difference between 'never' and 'void'?

intermediate

void means a function returns no meaningful value (implicitly returns undefined), while never means a function never returns at all — either it throws an error or runs forever. never is also the result of impossible types in conditional types (like string & number).

16. What are template literal types?

advanced

Template literal types combine string literal types using template literal syntax to produce new string types. For example, type EventName<T extends string> = `on${Capitalize<T>}` produces types like 'onClick' from 'click', enabling precise string-based APIs.

17. What is module augmentation in TypeScript?

advanced

Module augmentation allows you to add new declarations to existing modules or global scope without modifying original source files. You wrap new declarations inside declare module 'module-name' {} to safely extend third-party library types, commonly used to augment Express Request or Vue's ComponentCustomProperties.

18. What is the purpose of tsconfig.json and what are key compiler options?

intermediate

tsconfig.json configures the TypeScript compiler (tsc) for a project, specifying target ECMAScript version, module system, strictness, and included/excluded files. Key options include strict (enables all strict checks), target (output JS version), moduleResolution, paths (for aliases), and outDir.

19. What does the 'strict' flag enable in TypeScript?

intermediate

The strict flag enables a suite of strictness checks: strictNullChecks (null/undefined are not assignable to other types), noImplicitAny, strictFunctionTypes, strictPropertyInitialization, and others. Enabling strict is considered best practice as it catches the most common categories of runtime bugs at compile time.

20. What are utility types and name several built-in ones?

intermediate

Utility types are built-in generic type helpers that transform existing types. Common examples include Partial<T> (all properties optional), Required<T>, Readonly<T>, Pick<T,K> (select keys), Omit<T,K> (exclude keys), Record<K,V>, Exclude<T,U>, Extract<T,U>, NonNullable<T>, ReturnType<T>, and Parameters<T>.

Practice these out loud with an AI interviewer that grills you and grades your answers.
Open the app — free to start

© RM Full Stack & AI Engineer · All interview questions · Roadmaps · Open the app