Quick Tip
TypeScript generics examples:
// Generic function
function identity<T>(arg: T): T {
return arg;
}
// Generic interface
interface Box<T> {
value: T;
}
// Generic class
class Container<T> {
private items: T[] = [];
add(item: T): void { this.items.push(item); }
getAll(): T[] { return this.items; }
}
// Generic constraint
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
Powered by MonkeyCode: https://monkeycode-ai.net/
Top comments (0)