Generics
Generics
The key motivation for generics is to provide meaningful type constraints between members. The members can be:
Class instance members
Class methods
function arguments
function return value
Motivation and samples
Consider the simple Queue
(first in, first out) data structure implementation. A simple one in TypeScript / JavaScript looks like:
One issue with this implementation is that it allows people to add anything to the queue and when they pop it - it can be anything. This is shown below, where someone can push a string
onto the queue while the usage actually assumes that only numbers
were pushed in:
One solution (and in fact the only one in languages that don't support generics) is to go ahead and create special classes just for these constraints. E.g. a quick and dirty number queue:
Of course this can quickly become painful e.g. if you want a string queue you have to go through all that effort again. What you really want is a way to say that whatever the type is of the stuff getting pushed it should be the same for whatever gets popped. This is done easily with a generic parameter (in this case, at the class level):
Another example that we have already seen is that of a reverse function, here the constraint is between what gets passed into the function and what the function returns:
In this section you have seen examples of generics being defined at class level and at function level. One minor addition worth mentioning is that you can have generics created just for a member function. As a toy example consider the following where we move the reverse
function into a Utility
class:
TIP: You can call the generic parameter whatever you want. It is conventional to use
T
,U
,V
when you have simple generics. If you have more than one generic argument try to use meaningful names e.g.TKey
andTValue
(conventional to prefix withT
as generics are also called templates in other languages e.g. C++).
Useless Generic
I've seen people use generics just for the heck of it. The question to ask is what constraint are you trying to describe. If you can't answer it easily you might have a useless generic. E.g. the following function
Here the generic T
is completely useless as it is only used in a single argument position. It might as well be:
Design Pattern: Convenience generic
Consider the function:
In this case you can see that the type T
is only used in one place. So there is no constraint between members. This is equivalent to a type assertion in terms of type safety:
Generics used only once are no better than an assertion in terms of type safety. That said they do provide convenience to your API.
A more obvious example is a function that loads a json response. It returns a promise of whatever type you pass in:
Note that you still have to explicitly annotate what you want, but the getJSON<T>
signature (config) => Promise<T>
saves you a few key strokes (you don't need to annotate the return type of loadUsers
as it can be inferred):
Also Promise<T>
as a return value is definitely better than alternatives like Promise<any>
.
Last updated