Generics
Generics allow you to create templates for your types.
Example:
By using Generics you can relate an input parameter to an output parameter. The following code sets the type variable (T
) to number
when calling the generic function combine
:
1 |
|
TypeScript supports type argument inference when passing the input values:
1 |
|
You can also specify multiple type variables:
1 |
|
Default type variables are supported as well:
1 |
|
It is also possible to enforce a certain structure on your generic types using the extends
keyword:
1 |
|
Generics also work in classes:
1 |
|