function identity<T>(arg: T): T {
return arg;
}
let output = identity<string>("myString"); // output is string
let output2 = identity<number>(123); // output2 is numberTypeScript & Object-Oriented Design Patterns - Generics, Decorators & SOLID
Master advanced TypeScript features like generics, conditional types, and decorators, alongside essential Object-Oriented Design Patterns and SOLID principles for building robust, scalable software.
Cards in this deck
(20 cards)Preview terms and definitions before starting your study session.
T to extend another type, ensuring that T has at least the properties of that type. This allows you to operate on those properties within the generic function or class.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 }); // OK
// loggingIdentity(3); // Error: Argument of type 'number' is not assignable to parameter of type 'Lengthwise'.parameterName is Type. This tells the TypeScript compiler that if the function returns true, the parameter is of that specific type within the scope where the guard was called.interface Bird { fly(): void; layEggs(): void; }
interface Fish { swim(): void; layEggs(): void; }
function isFish(pet: Bird | Fish): pet is Fish {
return (pet as Fish).swim !== undefined;
}
function getSmallPet(): Fish | Bird {
return Math.random() < 0.5 ? { fly: () => {}, layEggs: () => {} } : { swim: () => {}, layEggs: () => {} };
}
let pet = getSmallPet();
if (isFish(pet)) {
pet.swim(); // TypeScript knows 'pet' is Fish here
} else {
pet.fly(); // TypeScript knows 'pet' is Bird here
}T extends U ? X : Y. If type T is assignable to type U, then the type is X; otherwise, it's Y.type IsString<T> = T extends string ? "Yes" : "No";
type A = IsString<string>; // type A = "Yes"
type B = IsString<number>; // type B = "No"
type TypeName<T> =
T extends string ? "string" :
T extends number ? "number" :
T extends boolean ? "boolean" :
T extends undefined ? "undefined" :
T extends Function ? "function" :
"object";
type C = TypeName<() => void>; // type C = "function"
type D = TypeName<null>; // type D = "object"infer keyword enhance conditional types?infer keyword allows you to declare a type variable within the extends clause of a conditional type, which can then be used in the "true" branch. It's used to extract a type from another type.type ReturnType<T> = T extends (...args: any[]) => infer R ? R : any;
function greet(name: string): string {
return `Hello, ${name}`;
}
type GreetReturnType = ReturnType<typeof greet>; // type GreetReturnType = string
type ArrayElementType<T> = T extends (infer U)[] ? U : T;
type NumArrayElement = ArrayElementType<number[]>; // type NumArrayElement = number
type StringElement = ArrayElementType<string>; // type StringElement = string[P in K] clause, where P is a new property name and K is a union of property keys.type Point = { x: number; y: number; };
// Makes all properties of Point optional
type PartialPoint = {
[P in keyof Point]?: Point[P];
};
// type PartialPoint = { x?: number; y?: number; }
// Makes all properties of Point readonly
type ReadonlyPoint = {
readonly [P in keyof Point]: Point[P];
};
// type ReadonlyPoint = { readonly x: number; readonly y: number; }Partial<T> do?Partial<T> is a built-in utility type that constructs a type with all properties of T set to optional. This is useful when you want to create an object that might only have a subset of the properties of an existing type.interface Todo {
title: string;
description: string;
completed: boolean;
}
type PartialTodo = Partial<Todo>;
/*
type PartialTodo = {
title?: string;
description?: string;
completed?: boolean;
}
*/
const updateTodo = (todo: Todo, fieldsToUpdate: PartialTodo): Todo => {
return { ...todo, ...fieldsToUpdate };
};
const myTodo: Todo = { title: "Learn TS", description: "Advanced topics", completed: false };
const updated = updateTodo(myTodo, { completed: true });
// updated is { title: "Learn TS", description: "Advanced topics", completed: true }Pick<T, K> do?Pick<T, K> is a built-in utility type that constructs a type by picking a set of properties K (a union of string literals) from type T. It creates a new type containing only the specified properties.interface User {
id: number;
name: string;
email: string;
age: number;
}
type UserProfile = Pick<User, "name" | "email">;
/*
type UserProfile = {
name: string;
email: string;
}
*/
const user: UserProfile = {
name: "Alice",
email: "[email protected]"
};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.