TypeScript Interview
Questions & Answers
π±Beginner QuestionsQ1βQ14
TypeScript is an open-source programming language developed by Microsoft (2012) that is a strict syntactical superset of JavaScript β it adds optional static typing to JS. TypeScript code compiles ("transpiles") to plain JavaScript that runs in any browser or Node.js environment.
- Static typing: Catch type errors at compile time rather than at runtime.
- Better IDE support: Autocompletion, inline docs, and refactoring tools work far better with type information.
- Safer refactoring: Change a function signature and the compiler instantly finds every broken call site.
- Self-documenting code: Type signatures act as built-in documentation β no need to read implementation details.
- ES features early: TypeScript supports modern JS features (decorators, enums) before they land in browsers.
interface and type in TypeScript?| Feature | interface | type |
|---|---|---|
| Object shapes | β Primary use case | β Yes |
| Declaration merging | β Yes (open) | β No |
| Extend/Implement | extends / implements | & intersection |
| Union types | β No | β Yes |
| Primitive aliases | β No | β Yes |
| Tuple types | β No | β Yes |
| Computed keys | β Limited | β Yes (mapped types) |
interface for object shapes that may need extending or implementing. Use type for unions, intersections, tuples, and primitives. For most object types, either works β pick one convention and stick to it.TypeScript can automatically infer the type of a variable from its initial value β you don't need to explicitly annotate every type. This keeps code clean without sacrificing safety.
const enum if you do use enums to avoid generating a runtime object.any, unknown, and never?| Type | Assignable from | Assignable to | Operations |
|---|---|---|---|
any | Everything | Everything | All allowed β disables type checking |
unknown | Everything | Only any/unknown | Must narrow type first |
never | Nothing | Everything | No operations β type that never exists |
as) in TypeScript?Type assertions tell the TypeScript compiler "trust me, I know this value's type better than you do." They are checked at compile time but have no runtime effect β no actual conversion happens.
tsconfig.json and what are the key compiler options?strictNullChecks and why is it important?With strictNullChecks enabled (strongly recommended), null and undefined are not automatically assignable to every type. You must explicitly handle them β preventing the dreaded "Cannot read properties of null" runtime error.
Function overloads let you define multiple type signatures for one function β useful when a function behaves differently based on argument types, but the actual implementation is a single function.
.d.ts)?Declaration files (.d.ts) contain only type information β no runtime code. They describe the shape of JavaScript libraries so TypeScript can type-check code that uses them.
β‘Intermediate QuestionsQ15βQ28
Generics allow you to write reusable, type-safe code that works with any type, without sacrificing type information. They're like type parameters β placeholders filled in when you use the function or class.
Type narrowing is the process of refining a type within a conditional block based on a runtime check. TypeScript's control flow analysis tracks which types are possible at each point in the code.
Mapped types transform the properties of an existing type by iterating over its keys. They're the foundation of many utility types like Partial, Readonly, and Pick.
Conditional types select one of two types based on a condition β following the pattern: T extends U ? X : Y. They're the type-level equivalent of ternary expressions.
Template literal types (TypeScript 4.1+) build new string types by combining literal types using template literal syntax β like tagged template literals but at the type level.
Decorators are a special kind of declaration that can be attached to classes, methods, properties, or parameters. They are functions that receive the target they decorate and can modify or annotate it. Used heavily in Angular, NestJS, and TypeORM.
keyof and typeof operators in TypeScript?TypeScript uses structural typing (duck typing) β if two types have the same shape, they're compatible, regardless of their name. This differs from languages like Java/C# which use nominal typing (types must explicitly declare compatibility).
satisfies operator in TypeScript?The satisfies operator (TypeScript 4.9+) validates that an expression matches a type without widening its type β you get type checking AND preserve the literal/specific type for use later.
Module augmentation lets you add types to existing modules β either third-party libraries or built-in TypeScript types β without modifying the original source.
π₯Advanced QuestionsQ29βQ40
TypeScript doesn't directly support higher-kinded types (type constructors as generic parameters), but they can be simulated using interface maps β a technique called "defunctionalization" or "type-level encoding".
Variance describes how a generic type's subtype relationships relate to its type argument's subtype relationships. TypeScript 4.7 added explicit variance annotations (in/out) for performance and clarity.
infer keyword and how do you use it?Type-safe builders use TypeScript's type system to ensure methods must be called in the correct order and required fields are set before the object can be built.
Branded types (also called opaque types) add a compile-time "tag" to primitive types to make distinct concepts incompatible with each other β even if their underlying type is the same.
Project references allow splitting a large TypeScript codebase into multiple independent sub-projects that can be compiled separately β improving build times and enforcing module boundaries in monorepos.
using keyword and explicit resource management?TypeScript 5.2 introduced using and await using β implementing the TC39 Explicit Resource Management proposal. Objects that implement [Symbol.dispose] are automatically cleaned up when the block exits.
- TS 5.0: Decorators (new standard β aligned with TC39 proposal),
consttype parameters (<const T>always infers literal types),verbatimModuleSyntaxflag, bundle size reduction (10β25% smaller tsc). - TS 5.1: Unrelated types for getter/setter,
undefinedas explicit return type, JSX transform improvements. - TS 5.2:
using/await using(Explicit Resource Management), decorator metadata,@types/nodeimprovements. - TS 5.3: Import attributes,
resolution-modefor imports, narrowing improvements forswitch(true). - TS 5.4: Preserved narrowing in closures after last assignment,
NoInfer<T>utility type. - TS 5.5: Inferred type predicates (functions that return booleans auto-infer type predicates),
--isolatedDeclarationsflag for parallel declaration emit.
Up Next: System Design
You've covered TypeScript. Next in the expanded series is System Design β scalability, load balancing, caching, databases, and real-world architecture patterns.
β SQL & Databases Q&A