TypeScript - Advanced Type Systems, Generics & Design Patterns
Cards in this deck
(25 cards)Preview terms and definitions before starting your study session.
getFirstElement that takes an array of any type and returns the first element, or undefined if the array is empty.function getFirstElement<T>(arr: T[]): T | undefined {
return arr.length > 0 ? arr[0] : undefined;
}
// Usage:
const numbers = getFirstElement([1, 2, 3]); // numbers is type number | undefined
const strings = getFirstElement(['a', 'b']); // strings is type string | undefinedBox<T> that can hold an item of any specified type T.interface Box<T> {
value: T;
}
// Usage:
const stringBox: Box<string> = { value: 'Hello' };
const numberBox: Box<number> = { value: 123 };function printId(id: string | number) {
if (typeof id === 'string') {
// id is narrowed to 'string' here
console.log(id.toUpperCase());
} else {
// id is narrowed to 'number' here
console.log(id.toFixed(2));
}
}typeof operator act as a type guard?typeof operator is a built-in JavaScript operator that returns a string indicating the type of the unevaluated operand. In TypeScript, it acts as a type guard for primitive types ('string', 'number', 'boolean', 'symbol', 'undefined', 'bigint'). When used in a conditional block, TypeScript narrows the type of the variable accordingly.if (typeof value === 'string') { /* value is string */ }instanceof operator act as a type guard?instanceof operator is a built-in JavaScript operator that checks if an object is an instance of a particular class. In TypeScript, it acts as a type guard for class instances. If the condition value instanceof MyClass is true, TypeScript narrows the type of value to MyClass within that block.class Dog { bark() {} }
class Cat { meow() {} }
function makeSound(animal: Dog | Cat) {
if (animal instanceof Dog) {
animal.bark(); // animal is narrowed to Dog
} else {
animal.meow(); // animal is narrowed to Cat
}
}switch statements or if/else chains.type property.kind property.interface Circle {
kind: 'circle';
radius: number;
}
interface Square {
kind: 'square';
sideLength: number;
}
type Shape = Circle | Square;
function getArea(shape: Shape): number {
switch (shape.kind) {
case 'circle':
return Math.PI * shape.radius ** 2; // shape is Circle
case 'square':
return shape.sideLength ** 2; // shape is Square
}
}Related Flashcard Sets
Explore similar study materials created by the community
TypeScript & Advanced Type Systems - Generics, Utility Types & Design Patterns
Master advanced TypeScript mechanics including Generics, Utility Types, Conditional Types, Mapped Types, and Type Narrowing. Designed for engineering interview prep and building bulletproof software architectures.
Harvard ECON1011 - Intermediate Microeconomics: Game Theory & Market Structures
Master key concepts in intermediate microeconomics with this comprehensive flashcard deck. Dive deep into game theory, market structures, strategic pricing, and general equilibrium models, perfect for Harvard ECON1011 students and anyone aiming to ace microeconomics.
MIT 18.03 - Differential Equations: Ordinary Differential Equations, Laplace & Fourier
Master MIT 18.03 Differential Equations with high-yield flashcards covering first and second-order ODEs, Laplace transforms, Fourier series, and matrix exponential state-space systems.
CPA Exam FAR - Financial Accounting & Reporting: US GAAP & Revenue Recognition
Master high-yield CPA FAR concepts covering ASC 606 revenue recognition, ASC 842 lease accounting, inventory valuation methods, and consolidated financial statements.