Generics
Generics allows us to create a component that can work over a variety of types rather than a single one.
Hello World of Generics
The identity function is a function that will return back whatever is passed in.
Without generics, we actually are losing the information about what that type was when the function returns.
We could use a type variable, which works on types rather than values.
Working with Generic Type Variables
We could use T[]
to denote an array of variables with type T
.
Generic Types
The type of generic functions:
Generic Interface:
We may want to move the generic parameter to be a parameter of the whole interface.
Instead of describing a generic function, we now have a non-generic function signature that is a part of a generic type. When we use GenericIdentityFn
, we now will also need to specify the corresponding type argument (here: number
), effectively locking in what the underlying call signature will use.
Generic Classes
Generic classes have a generic type parameter list in angle brackets (<>
) following the name of the class.
Generic classes are only generic over their instance side rather than their static side, so static members can not use the class's type parameter.
Generic Constraints
Instead of working with any and all types, we’d like to constrain this function to work with any and all types that also have the .length
property.
Using Type Parameters in Generic Constraints
Using Class Types in Generics
When creating factories in TypeScript using generics, it is necessary to refer to class types by their constructor functions.
Last updated