TypeScript Core - Advanced Generics, Utility Types & Type Inference
Master advanced TypeScript concepts with this high-yield flashcard deck, covering powerful generics, conditional and mapped types, essential utility types, and robust type inference techniques for building scalable and type-safe applications.
Cards in this deck
(23 cards)Preview terms and definitions before starting your study session.
extends keyword. This ensures that the generic type must either be, or extend from, a specified type. This allows you to apply operations specific to the constrained type within your generic code.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;
}T extends U ? X : Y, meaning 'if type T is assignable to type U, then the type is X; otherwise, it's Y'. They are fundamental for creating advanced utility types and type transformations.infer keyword used for within conditional types?infer keyword is used within the extends clause of a conditional type to declare a new type variable that can be 'inferred' from the type being checked. It allows you to extract parts of a type into a new type variable for further use. For example, infer R can extract the return type of a function type: type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;T that is a union (e.g., A | B | C), and T is a 'naked' type parameter (not wrapped in an array or other type), the conditional type becomes distributive. This means the conditional type is applied to each member of the union individually, and the results are then combined back into a new union type.type MyType<T> = T extends string ? 'isString' : 'notString';MyType<string | number> would resolve to MyType<string> | MyType<number>, which is 'isString' | 'notString'.[P in K] syntax, where K is typically keyof T. Their primary use case is to create variations of existing types, such as making all properties optional, readonly, or changing their types, often used in conjunction with utility types.keyof operator and provide an example.keyof operator takes an object type and produces a string or string literal union of its property keys. It's commonly used with mapped types to iterate over the keys of an existing type.interface User { id: number; name: string; age: number; }
type UserKeys = keyof User; // 'id' | 'name' | 'age'
function getProperty<T, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
const user: User = { id: 1, name: 'Alice', age: 30 };
const userName = getProperty(user, 'name'); // Type: stringas clause within the property signature. This allows you to map an old property key to a new one, creating a new type with transformed keys.type Event = { id: string; timestamp: Date; payload: any; };
type EventLog<T> = { [K in keyof T as `log${Capitalize<string & K>}`]: T[K] };
type LoggedEvent = EventLog<Event>;
// Result: { logId: string; logTimestamp: Date; logPayload: any; }Related Flashcard Sets
Explore similar study materials created by the community
JLPT N2 - Upper-Intermediate Japanese Kanji, Grammar & Expressions
Master high-frequency JLPT N2 grammar patterns, critical reading transition words, and high-yield kanji compounds to boost your exam score.
CompTIA Network+ (N10-008) - Networking Fundamentals & Subnetting
Master key networking concepts, OSI layer troubleshooting, IPv4/IPv6 subnetting calculations, critical port numbers, and Wi-Fi standards for the CompTIA Network+ N10-008 exam.
HSK 4 - Intermediate Chinese Vocabulary & Grammar
Master HSK 4 Chinese with this comprehensive flashcard deck! Dive into essential vocabulary, advanced grammar patterns like 把 and 被, common idioms, and cultural nuances to confidently elevate your conversational and reading skills.
CompTIA Network+ (N10-008) - Networking Fundamentals & Protocols
Master essential networking protocols, topologies, cabling standards, and troubleshooting tools with this high-yield CompTIA Network+ (N10-008) flashcard deck. Perfect for IT professionals and exam candidates aiming for certification success.