November 2018
Beginner
502 pages
10h 22m
English
We can make a whole class generic. Let's dive into an example of a generic class that stores data in a list:
class List<T> {}
We mark the class as generic by putting <T> after the class name.
private data: T[] = [];
We refer to the generic type using T. In our example, our data property is an array of whatever type the class has been declared with.
public getList(): T[] { return this.data;}
We reference the generic array as the return type with T[].
public add(item: T) { this.data.push(item); ...Read now
Unlock full access