Current location - Quotes Website - Signature design - Generics in TS
Generics in TS
Generics can be understood as generalized types, which are usually used for classes and functions.

Generics can be used for classes and constructors, such as:

As mentioned above, < t > means to pass a T type, and pass in a specific type when it is new. T is a variable that can be changed, but it is usually written as t.

When we talk about TypeScript, we talk about arrays. In fact, array is essentially a generic class.

Generics can be used for ordinary functions, such as:

In fact, whether it is used for classes or functions, the core idea is: pass in types as special parameters.

It should be noted that generics can also be "inherited", but the scope it represents is limited.

take for example

The above is the basic usage of generics!