TypeScript has become the standard for production JavaScript applications. Interviewers at companies like Google, Meta, Stripe, and Airbnb frequently ask TypeScript-specific questions for frontend and full-stack roles. This guide covers the most common TypeScript interview questions with in-depth answers.
Basic TypeScript Questions
1. What is TypeScript and how is it different from JavaScript?
TypeScript is a statically typed superset of JavaScript developed by Microsoft. Key differences:
| Feature | JavaScript | TypeScript |
|---------|-----------|------------|
| Type System | Dynamic (runtime) | Static (compile-time) |
| Compilation | Interpreted | Compiled to JS |
| Error detection | Runtime | Compile time |
| IDE support | Limited | Full IntelliSense |
| Interfaces | No | Yes |
| Generics | No | Yes |
// JavaScript - error caught only at runtimefunction add(a, b) { return a + b; }add("1", 2); // "12" — silent bug// TypeScript - error caught at compile time
add("1", 2); // Error: Argument of type 'string' is not assignable to parameter of type 'number'
2. What are the primitive types in TypeScript?
TypeScript has the following primitives: string, number, boolean, bigint, symbol, undefined, null.
let name: string = "Alice";let age: number = 30;let isActive: boolean = true;let bigNum: bigint = 9007199254740993n;let sym: symbol = Symbol("id");let nothing: undefined = undefined;let empty: null = null;
3. What is the difference between interface and type?
Both define shapes, but have key differences:
// interface — can be merged (declaration merging), better for OOPinterface User { id: number; name: string;}interface User { // merges with above email: string;}// Effective type: { id: number; name: string; email: string }// type alias — more flexible, supports unions/intersectionstype ID = string | number;type User = { id: ID; name: string };type Admin = User & { role: string }; // intersection// When to use each:// interface → public API shapes, class implementations, declaration merging// type → unions, tuples, mapped types, computed types
4. Explain unknown vs any vs never
// any — disables type checking (avoid in production)let val: any = "hello";val.toUpperCase(); // No error, but risky// unknown — type-safe any; must narrow before uselet data: unknown = fetchData();if (typeof data === "string") { data.toUpperCase(); // OK — narrowed}// never — function never returns (throws or infinite loop)function fail(msg: string): never { throw new Error(msg);}// never also appears in exhaustive checkstype Shape = "circle" | "square";function area(s: Shape): number { if (s === "circle") return 3.14; if (s === "square") return 1; const _exhaustive: never = s; // Error if new shape added return _exhaustive;}
Intermediate TypeScript Questions
5. What are generics and why are they useful?
Generics enable reusable, type-safe code that works with multiple types.
// Without generics — loses type infofunction identity(arg: any): any { return arg; }// With generics — preserves typefunction identity<T>(arg: T): T { return arg; }const str = identity("hello"); // type: stringconst num = identity(42); // type: number// Generic constraintsfunction getLength<T extends { length: number }>(arg: T): number { return arg.length;}getLength("hello"); // OKgetLength([1, 2, 3]); // OKgetLength(42); // Error: number has no length// Generic interfacesinterface Repository<T> { findById(id: number): T; findAll(): T[]; save(entity: T): void;}// Multiple generic paramsfunction pair<K, V>(key: K, value: V): [K, V] { return [key, value];}
// Bad — bypasses safetyconst input = document.getElementById("input") as HTMLInputElement;// Good — with null checkconst el = document.getElementById("input");if (!(el instanceof HTMLInputElement)) throw new Error("Expected input element");const input = el; // typed as HTMLInputElement
❌ Forgetting to use strict mode
Always enable "strict": true in tsconfig.json. It enables noImplicitAny, strictNullChecks, strictFunctionTypes, and more.
Quick Reference: Key Concepts
| Concept | Use Case |
|---------|----------|
| interface | Object shapes, class contracts, mergeable |
| type | Unions, intersections, computed types |
| generics | Reusable type-safe logic |
| unknown | Safe alternative to any |
| never | Exhaustive checks, unreachable code |
| Partial<T> | Optional updates (PATCH requests) |
| Readonly<T> | Immutable state, constants |
| Pick/Omit | API response shaping |
| ReturnType<F> | Infer return types |
| satisfies | Validate without widening |
Practice TypeScript in a real environment with our TypeScript coding challenges. Master generics, type manipulation, and advanced patterns hands-on.