December 2017
Beginner
372 pages
10h 32m
English
Generic classes are very similar to generic functions in the fact that they also use type parameters to specify the generic type. Let's suppose we want a class whose purpose is to manage the data in a collection, with operations such as adding the data to a collection and, then, retrieving the data from a collection. Now, a collection can be of any type, be it a string or a number.
This becomes a use case for generic classes, wherein we can define a class and assign a type parameter to the class. Then, we can use this type parameter as a concrete type in the different functions implemented in that class. Check out the following example:
class GenericClass <T>{ items :T[] = []; pushData(val: T){ this.items.push(val); } getData(index ...
Read now
Unlock full access