23 cards · Public · Shuffle

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.

#1
Term
What are TypeScript Generics and why are they useful?
Definition
TypeScript Generics allow you to write components that can work with a variety of data types, rather than a single one. They enable you to create reusable code that is both flexible and type-safe, ensuring type consistency across different parts of your application without sacrificing flexibility. They are often used with functions, classes, and interfaces.
#2
Term
How do you constrain a generic type parameter in TypeScript?
Definition
You can constrain a generic type parameter using the 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.
typescript5 lines
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;
}
#3
Term
Explain the basic syntax and purpose of TypeScript's conditional types.
Definition
Conditional types allow you to choose one of two possible types based on a condition that checks the relationship between two types. Their syntax is 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.
#4
Term
What is the infer keyword used for within conditional types?
Definition
The 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;
#5
Term
Describe how conditional types behave when applied to union types (Distributive Conditional Types).
Definition
When a conditional type operates on a generic type 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.
Example: type MyType<T> = T extends string ? 'isString' : 'notString';
MyType<string | number> would resolve to MyType<string> | MyType<number>, which is 'isString' | 'notString'.
#6
Term
What are Mapped Types in TypeScript and what is their primary use case?
Definition
Mapped Types are a powerful feature that allows you to create new object types by transforming the properties of an existing object type. They iterate over the properties of a type using a [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.
#7
Term
Explain the keyof operator and provide an example.
Definition
The 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.
typescript9 lines
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: string
#8
Term
How can you rename properties in a mapped type?
Definition
You can rename properties in a mapped type using the as 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.
typescript5 lines
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; }
Showing 8 of 23 cards in this deck.
Study All Now