TypeScript - Advanced Type System, Generics & Utility Types
Partial<T> and Record<K, T> to write more robust and flexible code.Cards in this deck
(22 cards)Preview terms and definitions before starting your study session.
function identity<T>(arg: T): T {
return arg;
}
let output1 = identity<string>("myString"); // type of output1 is string
let output2 = identity(123); // type of output2 is number (type inference)extends keyword, allowing you to access properties or methods of the constrained type.interface Lengthwise {
length: number;
}
function loggingIdentity<T extends Lengthwise>(arg: T): T {
console.log(arg.length); // Now we know 'arg' has a .length property
return arg;
}
loggingIdentity({ length: 10, value: 3 });
// loggingIdentity(3); // Error: Argument of type 'number' is not assignable to parameter of type 'Lengthwise'.for...in like syntax.[P in K], where P is a new property name and K is a union of string literal types (often keyof T).type MyMappedType<T> = {
[P in keyof T]: T[P]; // Iterates over properties of T, keeping their original types
};? modifier to the property type within the mapped type. This is how the built-in Partial<T> utility type works.type Partial<T> = {
[P in keyof T]?: T[P]; // Makes all properties optional
};readonly modifier to the property within the mapped type. This is how the built-in Readonly<T> utility type works.type Readonly<T> = {
readonly [P in keyof T]: T[P]; // Makes all properties readonly
};Partial<T> utility type.Partial<T> constructs a type with all properties of T set to optional. It's useful when you need to create an object that might only contain a subset of the properties of an existing type, for example, when updating an entity.interface User {
id: number;
name: string;
email: string;
}
type UserUpdate = Partial<User>;
// UserUpdate is equivalent to: { id?: number; name?: string; email?: string; }Related Flashcard Sets
Explore similar study materials created by the community
TypeScript - Advanced Types & Tooling
Master TypeScript's advanced type system, from powerful generics and flexible utility types to robust type guards. Optimize your development workflow by understanding key `tsconfig.json` options and the nuances between `interface` and `type`.
TypeScript - Advanced Types, Generics & Utility Types
Master advanced TypeScript type system mechanics including conditional types, mapped types, utility types, and custom type guards to write bulletproof, flexible type definitions.
TypeScript - Advanced Type Systems, Generics & Design Patterns
Master advanced TypeScript features like generics, conditional types, and mapped types to build robust, scalable applications. This deck covers essential utility types, type narrowing techniques, discriminated unions, and the nuances between interfaces and type aliases, empowering you to write highly maintainable and type-safe code.
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.