Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Generic Interfaces and Classes | Working with Generics
Working with Interfaces and Generics in TypeScript

bookGeneric 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" };
copy

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]
copy

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.

question mark

What is the main advantage of using generic interfaces and classes with type parameters in TypeScript?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 9.09

bookGeneric Interfaces and Classes

Swipe to show menu

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" };
copy

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]
copy

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.

question mark

What is the main advantage of using generic interfaces and classes with type parameters in TypeScript?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 3
some-alt