Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Generic Methods in Classes | Generics in Classes
Quizzes & Challenges
Quizzes
Challenges
/
TypeScript Classes and OOP

bookGeneric Methods in Classes

123456789
class DataProcessor { processItem<T>(item: T): string { return `Processing item: ${item}`; } } const processor = new DataProcessor(); console.log(processor.processItem<number>(42)); // Processing item: 42 console.log(processor.processItem<string>("hello")); // Processing item: hello
copy

You often need flexibility in your class methods, especially when a method should work with a variety of types. In these situations, you can define a generic method within a class. A generic method allows you to specify a type parameter only for that method, rather than for the entire class. This is particularly useful when only one or a few methods require type flexibility, while the rest of the class remains type-specific.

By contrast, a generic class makes the entire class generic, meaning all properties and methods can use the generic type parameter. You should use a generic class when the type flexibility is needed across the whole class—for example, when storing or managing collections of different types.

Choose a generic method when only a specific method needs to be generic, and use a generic class when the entire class or most of its members need to work with a generic type.

question mark

When should you use a generic method instead of a generic class?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 5

bookGeneric Methods in Classes

Desliza para mostrar el menú

123456789
class DataProcessor { processItem<T>(item: T): string { return `Processing item: ${item}`; } } const processor = new DataProcessor(); console.log(processor.processItem<number>(42)); // Processing item: 42 console.log(processor.processItem<string>("hello")); // Processing item: hello
copy

You often need flexibility in your class methods, especially when a method should work with a variety of types. In these situations, you can define a generic method within a class. A generic method allows you to specify a type parameter only for that method, rather than for the entire class. This is particularly useful when only one or a few methods require type flexibility, while the rest of the class remains type-specific.

By contrast, a generic class makes the entire class generic, meaning all properties and methods can use the generic type parameter. You should use a generic class when the type flexibility is needed across the whole class—for example, when storing or managing collections of different types.

Choose a generic method when only a specific method needs to be generic, and use a generic class when the entire class or most of its members need to work with a generic type.

question mark

When should you use a generic method instead of a generic class?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 3
some-alt