Type vs InterfaceProblem Statement'When should you use "interface" vs "type"? Give one example where only "type" works.'Approach 1. Interfaces are better for objects and classes. Types are needed for unions and primitives. Solution// Interfaces can merge (declaration merging). // Types cannot merge. // Union types only work with 'type': type Status = "ready" | "waiting"; // interface Status ... Error!
Any vs UnknownProblem Statement'What is the difference between "any" and "unknown"?'Approach 1. `any` disables type checking. `unknown` requires narrowing before use. Solutionlet a: any = 10; a.toUpperCase(); // OK at compile time (fails at runtime) let b: unknown = "hello"; // b.toUpperCase(); // Error! if (typeof b === "string") b.toUpperCase(); // OK
The Never TypeProblem Statement'Write a function that would have a "never" return type.'Approach 1. Functions that throw or have infinite loops return `never`. Solution`function fail(): never { throw new Error("Failure");
Implementing RecordProblem Statement'Explain how the Record<K, T> utility type is implemented.'Approach` 1. It uses a mapped type: `{ [P in K]: TSolution`type MyRecord<K extends keyof any, T> = { [P in K]: T;
Distributive CheckProblem Statement'Why does T extends any ? T[] : never return string[] | number[] when passed string | number?'Approach 1. This is "Distributive Conditional Types". Solution// Conditional types are distributed over union types during instantiation. type Result = (string | number) extends any ? (string | number)[] : never; // Becomes: (string extends any ? string[] : never) | (number extends any ? number[] : never)
Extract KeysProblem Statement'How to extract the keys of an object as a union type?'Approach 1. Use `keyof`. Solution`interface User { id: number; name: string
Non-Empty ArrayProblem Statement'Define a type NonEmptyArray<T> using a tuple.'Approach 1. At least one element of type `T`, followed by any number of elements. Solutiontype NonEmptyArray<T> = [T, ...T[]];
Mutable to ReadonlyProblem Statement'Convert a mutable array number[] to a readonly tuple [1, 2, 3] type.'Approach 1. Use `as const`. Solutionconst arr = [1, 2, 3] as const; type ReadonlyArr = typeof arr;
Make Keys OptionalProblem Statement'Write a type OptionalKeys<T, K extends keyof T> that only makes specific keys optional.'Approach 1. Omit K from T, then Pick K from T and make them Partial, then Intersect. Solutiontype OptionalKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
Intersection ConflictProblem Statement'What happens if you intersect { a: stringApproach 1. The property `a` becomes `never` because a value cannot be both string and number. Solution`type Conflict = { a: string
Abstract Class PurposeProblem Statement'Can you instantiate an abstract class?'Approach 1. No, they serve as templates for other classes. Solution`abstract class Base {
TS Private accessProblem Statement'Is "private" in TS actually private at runtime?'Approach 1. No, it is removed after compilation. Only native JS # fields are private at runtime. Solution`class User { private id = 123;
Indexed AccessProblem Statement'How to get the type of a specific property in an interface?'Approach 1. Use `Interface["propName"]`. Solution`interface User { meta: { age: number
Remove OptionalityProblem Statement'What operator is used in mapped types to remove optionality?'Approach 1. The `-?` operator. Solution`type MyRequired<T> = { [P in keyof T]-?: T[P];
Enum Values UnionProblem Statement'How to get a union of all the VALUES of a string enum?'Approach` 1. Use `` ${EnumSolution`enum Status { A = "a", B = "b"
Type Assertion Use CaseProblem Statement'When should you use "as" type assertion?'Approach 1. When you know more about the type than TS (e.g. from an API or DOM). Solutionconst el = document.getElementById("my-id") as HTMLInputElement;
Void vs UndefinedProblem Statement'What is the difference between void and undefined as return types?'Approach 1. `void` says the function returns nothing useful (and allows return 1; if not used). 2. `undefined` requires an explicit return statement or it fails. Solution`function a(): void {
Typed CurryingProblem Statement'Type a simple curry2 function: (a: number, b: number) => number.'Approach 1. Returns a function that returns a function. Solutiontype Curry2 = (a: number) => (b: number) => number; const add: Curry2 = a => b => a + b;
Inferred Array TypeProblem Statement'How do you get the type of elements inside const arr = [1, "two"]?'Approach 1. Use `(typeof arr)[number]`. Solutionconst arr = [1, "two"]; type Element = (typeof arr)[number]; // string | number
Structural TypingProblem Statement'What kind of type system does TypeScript use?'Approach 1. Structural (Duck Typing). Solution`// If two objects have the same shape, they are the same type. class A { x = 0