These are only present in TS not in JS.

Built-in Generics & What are Generics?

Screenshot 2022-10-31 at 1.15.54 PM.png

This is a built in generic type. It says the names is an array of either strings or numbers.

Another generic type built in TS is the promise type. Promises are a JS feature. They can be created like:

Screenshot 2022-10-31 at 1.18.33 PM.png

Just note here if I hover over it, TS says it is of type Unknown because TS is not fully able to understand that it will actually resolve to a string here. So, we can do:

Screenshot 2022-10-31 at 1.21.00 PM.png

So, we get better type safety with generic types.

Generic Functions

One good example is: https://www.youtube.com/watch?v=RHah57-vv-E(Must watch video)

Screenshot 2022-10-31 at 2.00.04 PM.png

Note: Here I am getting error because TS does not know wheather it has age or not. One solution can be to use type casting like:

Screenshot 2022-10-31 at 2.01.12 PM.png

But there is an easier way using generics:

Screenshot 2022-10-31 at 2.24.55 PM.png

Here, by extending object on line 14 we are saying that the passed parameter should be an object. This is called Type Constraints.

Note: extends = type constraints.

Another example:

Screenshot 2022-11-01 at 12.29.13 PM.png