Interview Q&A / TypeScript
TypeScript interview questions
Types, interfaces, generics and safe patterns for real codebases.
Prefer rapid revision? Practise TypeScript short interview answers →
- What are generics in TypeScript and why are they useful?
Generics let you write a function, class or type that works with many types while still remembering which type was used. Instead of falling back to any, you add a type parameter like T, so the output type is linked to the input type and TypeScript can still check your code.
BeginnerIn-depth3 min read - What are utility types in TypeScript, such as Partial, Required, Pick, Omit and Record?
Utility types are built-in generic types that create a new type from an existing one. Partial makes all properties optional, Required makes them all required, Pick keeps only chosen keys, Omit removes chosen keys, and Record builds an object type from a set of keys and a value type.
BeginnerIn-depth3 min read - What is the difference between interface and type in TypeScript?
Both describe the shape of data. An interface describes objects and can be extended or merged; a type alias can name any type, including unions, tuples and primitives.
BeginnerIn-depth2 min read - What is the difference between union and intersection types in TypeScript?
A union type (A | B) means a value can be one of several types, so you can only use what they have in common until you narrow it. An intersection type (A & B) means a value must be all of the types at once, so it has every property from each.
BeginnerIn-depth3 min read - What is the difference between unknown and any in TypeScript?
Both can hold any value. any turns off type checking completely, while unknown forces you to check what the value is before you use it, so it is the safe choice.
BeginnerIn-depth2 min read - Should you use an enum or a union of string literals in TypeScript?
Both describe a fixed set of allowed values. An enum creates a real JavaScript object at runtime and its members must be referenced by name. A union of string literals like "admin" | "user" exists only at compile time, adds no code, and accepts plain strings. Many teams now prefer unions, or an as const object when they also need runtime values.
IntermediateIn-depth4 min read - What is type narrowing in TypeScript, and how do type guards work?
Narrowing is how TypeScript refines a broad type, like string | number, to a more specific one inside a block after a check. Type guards are those checks, such as typeof, instanceof, the in operator, equality checks on a shared field, and custom functions that return a type predicate like value is User.
IntermediateIn-depth3 min read - What is the never type in TypeScript, and how do you use it for exhaustive checks?
never is the type with no possible values. It is the return type of functions that never finish normally, like one that always throws, and it is what remains after every case of a union has been handled. Assigning the leftover value to never makes the compiler report any case you forgot.
AdvancedIn-depth4 min read