13.7 Generics
Like interfaces, generics represent a language feature of TypeScript that has no equivalent in JavaScript. Generics allow you to provide functions and classes with a placeholder type and assign a concrete type to it at a later time. Generics are often used in the context of collections, that is, collections of objects of the same type. In this case, the basic structure of the collection with its methods is initially independent of a concrete type. However, if you use the collection in your application, it must hold concrete types. You then define the type and get TypeScript support.
In Listing 13.28, you use class Collection<T> to specify that this should be a generic class. Within the class definition, you use the T variable ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access