TypeScript is a spell-checker for your code: it finds mistakes before running it (“this might be undefined”, “you passed a string where a number goes”). But it disappears when compiled to JavaScript, so it can’t check data arriving at runtime from an API or a form.
Say it like this in the interview
“Static types catch errors at compile time, give autocomplete and make refactoring safe. Types are erased at runtime, so external data like API responses must still be validated, for example with Zod, at the boundary.”
Traps that make people fail
Believing as User validates the data. It just silences the compiler.
any switches the spell-checker off: anything goes. unknown says “I don’t know what this is yet, so you must check before using it.”
function handle(value: unknown) {
value.toUpperCase(); // error
if (typeof value === 'string') {
value.toUpperCase(); // ok: narrowed to string
}
}
Say it like this in the interview
“any disables type checking and spreads silently. unknown is the type-safe top type: you must narrow it with typeof, instanceof or a type guard before use. I use unknown for untrusted data like JSON.parse results or catch errors.”
Both describe object shapes. interface can be extended and merged (declared twice and combined). type can also describe unions, tuples and computed types ('a' | 'b').
Say it like this in the interview
“For object shapes they’re mostly interchangeable. Interfaces support declaration merging and extends, good for public APIs. Type aliases handle unions, intersections, mapped and conditional types. I pick one convention per codebase, typically type for unions and interface for objects.”
It checks that a value matches a type without throwing away the more specific information TypeScript inferred.
Say it like this in the interview
“const config = { port: 3000 } satisfies Config validates against Config but keeps the literal inferred type, so I get both checking and precise autocomplete, unlike a type annotation that widens it.”
A union like 'small' | 'large' is just types, with no extra code. An enum creates a real JavaScript object at runtime.
Say it like this in the interview
“String literal unions are zero-cost and work well with JSON; enums generate runtime code and have quirks like reverse mappings for numeric enums. Many teams prefer unions or as const objects.”
It switches on the checks that catch the most bugs, especially “this might be null or undefined”.
Say it like this in the interview
“strictNullChecks, noImplicitAny, strictFunctionTypes and more. strictNullChecks alone prevents a large class of ‘cannot read property of undefined’ errors. New projects should always enable it.”
Generics are type variables: “this function works with any type T, and whatever T goes in, the same T comes out.” You get reuse without losing type safety.
function groupBy<T, K extends PropertyKey>(items: T[], key: (item: T) => K): Record<K, T[]> {
const groups = {} as Record<K, T[]>;
for (const item of items) {
(groups[key(item)] ??= []).push(item);
}
return groups;
}
const byStatus = groupBy(orders, o => o.status); // Record<string, Order[]>
Say it like this in the interview
“Generics parameterise types. T is the item type, inferred from the argument, and K is constrained to valid property keys. The return type is precise, unlike using any.”
Utility types build new types from existing ones, so you don’t copy-paste shapes.
type User = { id: number; name: string; email: string; password: string };
type PublicUser = Omit<User, 'password'>;
type UserUpdate = Partial<Pick<User, 'name' | 'email'>>;
type Roles = Record<'admin' | 'editor', string[]>;
type Data = Awaited<ReturnType<typeof fetchUser>>;
Say it like this in the interview
“Partial for update payloads, Pick and Omit to derive views of a model, like a public user without the password, Record for maps, ReturnType and Awaited to derive types from functions, Readonly for immutability.”
keyof User is the list of a type’s property names. User['email'] is the type of one property. Mapped types build a new type by transforming every property of another.
Say it like this in the interview
“keyof T gives a union of keys, T[K] the property type, and mapped types like { [K in keyof T]?: T[K] } transform each property. That’s how Partial, Readonly and Record are implemented.”
A union of shapes that share a label field (status). Checking the label tells TypeScript exactly which shape you have, so impossible states (“error with data”) can’t exist.
type Result =
| { status: 'loading' }
| { status: 'error'; message: string }
| { status: 'success'; data: User[] };
function render(r: Result) {
switch (r.status) {
case 'error': return r.message; // TS knows message exists
case 'success': return r.data.length;
case 'loading': return 'Loading…';
default: const never: never = r; // compile error if a case is missing
}
}
Say it like this in the interview
“A discriminated union uses a common literal property to distinguish variants. Switching on it narrows the type automatically, and a never check gives exhaustiveness checking, so adding a new variant breaks the build until it’s handled.”
never means “this can’t happen”. A function that always throws returns never; and after you’ve handled every case of a union, what’s left is never.
Say it like this in the interview
“never is the empty type: functions that never return, impossible branches, and exhaustiveness checks where assigning a leftover union member to never causes a compile error when a case is missed.”
A type guard is a function that checks something at runtime and tells TypeScript “if this returns true, the value is this type”.
type User = { id: number; email: string };
function isUser(value: unknown): value is User {
return typeof value === 'object' && value !== null &&
typeof (value as User).id === 'number' && typeof (value as User).email === 'string';
}
const data: unknown = await res.json();
if (isUser(data)) console.log(data.email);
Say it like this in the interview
“A function returning value is Type; after calling it in an if, TypeScript narrows the type. Useful for validating unknown API data, though schema libraries like Zod are more robust.”