TypeScript Interview Questions - Interview Prep
50 Questions Available
Comprehensive collection of 50 essential TypeScript interview questions covering types, interfaces, generics, advanced types, and TypeScript best practices. Free TypeScript interview questions with answers. TypeScript interview prep guide.
All Questions & Answers
What is TypeScript and what are its main features?
TypeScript is superset of JavaScript that adds static type checking. Main features: static typing, type inference, interfaces, classes, generics, enums, optional chaining, nullish coalescing. Compiles to JavaScript. Catches errors at compile-time, improves code quality and maintainability.
What is the difference between TypeScript and JavaScript?
TypeScript adds static typing to JavaScript. TypeScript has types, interfaces, generics, access modifiers. JavaScript is dynamically typed. TypeScript compiles to JavaScript. TypeScript catches errors at compile-time, JavaScript at runtime. TypeScript is optional typing, can gradually adopt.
What are the basic types in TypeScript?
Basic types: number, string, boolean, null, undefined, void, any, unknown, never, object, array, tuple, enum. number for numbers, string for text, boolean for true/false, any for any type, unknown for type-safe any, never for unreachable code.
What is the difference between interface and type?
Interface: can be extended, merged (declaration merging), better for object shapes. Type: can represent unions, intersections, primitives, more flexible. Use interface for object shapes, type for unions/intersections. Both can be used similarly for objects.
What is type inference?
Type inference automatically determines types without explicit annotations. TypeScript infers types from values, function return types, context. Use explicit types when inference is unclear or for documentation. let/const inference differs: const is literal, let is wider type.
What are generics?
Generics create reusable components that work with multiple types. Syntax: <T>. Placeholder for type, specified when used. Enables type-safe code without losing flexibility. Common in functions, interfaces, classes. Example: Array<T>, Promise<T>, Map<K, V>.
What is the difference between any and unknown?
any disables type checking, allows any operation, not type-safe. unknown is type-safe top type, requires type checking before use. Use any sparingly (legacy code), unknown for values of unknown type. unknown forces type guards, any does not.
What is the difference between void and never?
void means function returns nothing (undefined). never means function never returns (throws error, infinite loop). void for functions that don't return value. never for functions that never complete normally, exhaustive checks in switch statements.
What are union and intersection types?
Union type (|) represents value that can be one of several types. Intersection type (&) combines multiple types into one. Union: "string | number" means string OR number. Intersection: "A & B" means A AND B (has properties of both).
What are optional properties?
Optional properties can be undefined. Mark with ? after property name. Allows objects to omit properties. Access with optional chaining (?.) or check for undefined. Useful for flexible object shapes, function parameters.