November 2019
Beginner
804 pages
20h 1m
English
Because TypeScript supports generics, it can actually leverage them for its own libraries.
If you look at the lib.es5.d.ts (https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es5.d.ts) file (which is part of TypeScript) and search for Array<T>, you'll stumble upon this definition:
interface Array<T> {
...
push(...items: T[]): number;
pop(): T;
...
}
As you can see, the Array interface accepts a generic parameter (inside the angle brackets: <>) named T. This T is actually just a label used to refer to a type that will be defined/given at runtime. That same label is then used in function definitions.
If we observe the push method, we can see that it accepts an array of T elements. The pop method returns ...
Read now
Unlock full access