 Generic Interfaces and Classes
Generic Interfaces and Classes
When you want to write code that works with a variety of types, generic interfaces and classes in TypeScript give you a powerful way to build reusable, flexible patterns. Generics allow you to define the structure or behavior of an interface or class without committing to a specific type up front. Instead, you use a type parameter—commonly named T—that acts as a placeholder for any type you choose when you use the interface or class. This means you can write one definition and use it with many types, reducing code duplication and increasing type safety.
12345678// A generic interface for a box that holds a value of any type interface Box<T> { value: T; } // Using Box with different types const numberBox: Box<number> = { value: 123 }; const stringBox: Box<string> = { value: "hello" };
The Box<T> interface defines a shape for an object that contains a single property, value, whose type is determined by whatever type you provide for T. When you declare numberBox as Box<number>, the value property must be a number; when you declare stringBox as Box<string>, the value property must be a string. This pattern lets you create containers, wrappers, or other generic data structures that can be reused with any type you need.
123456789101112131415161718192021222324// A generic class for storing and retrieving items class Repository<T> { private items: T[] = []; add(item: T): void { this.items.push(item); } getAll(): T[] { return this.items; } } // Using Repository with strings const stringRepo = new Repository<string>(); stringRepo.add("apple"); stringRepo.add("banana"); console.log(stringRepo.getAll()); // Output: ["apple", "banana"] // Using Repository with numbers const numberRepo = new Repository<number>(); numberRepo.add(1); numberRepo.add(2); console.log(numberRepo.getAll()); // Output: [1, 2]
Generic interfaces and classes are especially useful in real-world scenarios where you need to handle data of different shapes but want to use the same logic for storing, retrieving, or processing them. For example, a generic Repository<T> class can be used to store users, products, or any other kind of entity, simply by specifying the type when you create an instance. This approach helps you avoid writing separate classes for each data type, making your codebase easier to maintain and extend as your application grows.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you explain how to use generics with functions in TypeScript?
What are some common use cases for generic interfaces and classes?
How do generics improve type safety in TypeScript?
Awesome!
Completion rate improved to 9.09 Generic Interfaces and Classes
Generic Interfaces and Classes
Veeg om het menu te tonen
When you want to write code that works with a variety of types, generic interfaces and classes in TypeScript give you a powerful way to build reusable, flexible patterns. Generics allow you to define the structure or behavior of an interface or class without committing to a specific type up front. Instead, you use a type parameter—commonly named T—that acts as a placeholder for any type you choose when you use the interface or class. This means you can write one definition and use it with many types, reducing code duplication and increasing type safety.
12345678// A generic interface for a box that holds a value of any type interface Box<T> { value: T; } // Using Box with different types const numberBox: Box<number> = { value: 123 }; const stringBox: Box<string> = { value: "hello" };
The Box<T> interface defines a shape for an object that contains a single property, value, whose type is determined by whatever type you provide for T. When you declare numberBox as Box<number>, the value property must be a number; when you declare stringBox as Box<string>, the value property must be a string. This pattern lets you create containers, wrappers, or other generic data structures that can be reused with any type you need.
123456789101112131415161718192021222324// A generic class for storing and retrieving items class Repository<T> { private items: T[] = []; add(item: T): void { this.items.push(item); } getAll(): T[] { return this.items; } } // Using Repository with strings const stringRepo = new Repository<string>(); stringRepo.add("apple"); stringRepo.add("banana"); console.log(stringRepo.getAll()); // Output: ["apple", "banana"] // Using Repository with numbers const numberRepo = new Repository<number>(); numberRepo.add(1); numberRepo.add(2); console.log(numberRepo.getAll()); // Output: [1, 2]
Generic interfaces and classes are especially useful in real-world scenarios where you need to handle data of different shapes but want to use the same logic for storing, retrieving, or processing them. For example, a generic Repository<T> class can be used to store users, products, or any other kind of entity, simply by specifying the type when you create an instance. This approach helps you avoid writing separate classes for each data type, making your codebase easier to maintain and extend as your application grows.
Bedankt voor je feedback!