For that again I'm back in our dummy project set up which we used over and over again in there we can
run and people start to bring up that development server and then in a new terminal window with the
server running in a different terminal window I'm running TSC dash W to start my typescript compilation
process and keep that up and running with that of course we got an empty page because there's nothing
on it but now we can play around in the apt yes file and get started with generics and I think it's
easiest to understand generics if we have a look at a default type which is built into typescript with
which we actually worked a lot which turns out to be a generic and that would be an array.
Let's say we have an array of names Max and manual.
Let's say we get two names in there.
Then of course the type of the names constant is an array of strings.
Now actually we could think of this as two types being combined just as you can have an object with
different properties where in every property you might be storing a different type an array also is
just a thing with different data in it.
In this case with strings in it.
So we actually have the array type just in array.
And if I would remove the names here and cut them you would see that times code would inferred as to
be an array of type any.
So with any data in there.
Indeed typescript knows the array type.
So we could say this year should be an array.
But as you see if I specified like this we get an error even though I can tell you that this array type
like this exists a typescript but we see that it's a generic type and that it requires one type argument.
And so that's something we haven't seen before.
We never heard about generic types before.
Now this strange notation here whenever you see something like this in typescript you're dealing with
a generic type a generic type is a type which is kind of connected with some other type and is really
flexible regarding which exact type that Abbott type is sounds difficult.
Well let's go back to that array example.
We know that we want to store an array here.
So an array itself could be a type.
It's a list of data that on its own already makes up a type.
You could say just like an object is a type on its own.
Even if we don't know yet which data will go in there.
So an array is a type on its own but an array of course stores data of a certain type in there.
Now the array type itself does not care which type of data is stored in there the array type does not
care whether you are storing a list of strings of numbers of objects or of mixed data the array type
really doesn't care but it does care about getting at least some information even if you are telling
it I don't know by setting this to type any array it's better than not specifying anything.
Now this is one way of defining an array type the type of data which is stored in there and then square
brackets the other way which would create exactly the same type would be to use that array thing and
then these angular brackets and now between these angled brackets you specify the type of data which
should go into the array for example a string and this year is 100 percent the same as defining this
type here.
Of course here you could all use a union type to say does arrays stores strings or numbers or you use
any if you don't want to say anything about the type or as we just had its string.
So this is a generic type a generic type built into typescript a type which is connected to another
type where we want to know which our type dead is.
So to typescript can give us better support because here for example if we noted what we store in there
will be a string.
Well then we know whenever I access an element in that array I can do string stuff with it.
I can call all these string methods for example and typescript will not complain because it knows well
this array holds strings.
That's the idea of generic types a certain type.
In this case the array type might simply work better or work at all if you provide an additional information
about a type of data that's provided in this array type.
And of course the array type is just one example.
Another generic type which is built into typescript is the promise type.
Now promises are javascript feature right.
It's not a typo.
Good feature.
You can create promises in JavaScript and in case you're not sure what promises are attached you find
a link.
A promise can be created by using new promise.
Again this is a constructor function a class built into javascript and the promise constructor function
takes one argument which itself is a function which then in turn will get to arguments eventually resolve
and a reject function.
Now these will be passed in automatically to us by JavaScript in the browser and then in a promise we
can for example set a timer and then in that callback function off that timer we could resolve the outer
promise.
So in here in this callback function we could call resolve and say this is done now when we do so.
This promise here creates a new object a promise object which is stored in the promised constant.
Now this promise constant actually has a specific type of type promise and then again you see these
angular brackets because this promise eventually will resolve to something.
Now here it says unknown because times Square does not fully able to understand that we actually will
resolve to a string here but therefore we could say this is a promise which eventually will yield a
string.
And we do so by again using our generic type.
The main type is promise but a promise just like an array kind of works to gather with other types an
array used other types because it's stored data in it.
Date of certain types and a promise works together with other types because eventually it kind of returns
some data of some type.
And in this case that's a promise which eventually returns a string.
Now again why is that useful why is this extra type information useful.
Well if we just had a promise type here and that would not be allowed but if we said promise of type
anything which would basically the same as if types could you wouldn't know generic types if it wouldn't
have any information about what we get back.
Well then on this promise of course we can use then as we can do and all promises and eventually we'll
get some data but we know nothing about that data.
I can't call a string method on it for example while I can because it's any in this case.
But I don't get any particular types support I could all the try stuff on it which definitely won't
work.
For example if we turns around and we return a number here I could still call a string method here because
typescript has no information about the data.
This promise will eventually yield now with generic types we give that information to typescript we
can tell typescript that this promise will eventually yield a string or in this case a number and then
times can warn us here.
If I try to save does it will give us an error because it knows hey this promise eventually will yield
a number and you can call split on that.
So we get better type safety with generic types.
So you're really flexible regarding what you do with that generic type information an array knows which
data it stores a promise knows which data it returns.
If you build your own generic classes or functions you might do something totally different in there
but in the end generic types help you to get additional type information if you've got a more complex
class or more complex function that does something with the data that's coming in in a way where it
doesn't really care about the data being of one particular type but where you want to store the type
information of the incoming data to get better typescript support.
Whenever you work with your generic types so whenever you work with that promise or that array that's
basically the idea of generics.
So this year all were built in generics.
Let's comment that out.
Let's now build our own generic type word.
Let's use our own generic types.