Generic Classes & Interfaces in TypeScript
From the TypeScript cheat sheet · Generics · verified Jul 2026
Generic Classes & Interfaces
Build flexible classes and interfaces with generic types
typescript
// Generic class
class Box<T> {
private value: T
constructor(value: T) {
this.value = value
}
getValue(): T {
return this.value
}
setValue(value: T): void {
this.value = value
}
}
const numberBox = new Box<number>(42)
const stringBox = new Box<string>("hello")
// Generic interface
interface Container<T> {
value: T
add(item: T): void
remove(): T | undefined
}💡 Generic classes create type-safe data structures
📌 Static methods can have their own generic parameters
✅ Use generic constraints for type safety