So we get the basics about generics down hopefully.
Now let's dive a bit deeper into constraints or into one specific type of constraint which we can add
retail trade yet a no function of generic function which will name extract and convert the idea of this
function here is dead.
We get an object as a first argument and the second parameter is in the end a key now want to return
to value for that key in the object which we can do with this default javascript notation using the
square brackets to access a property in object.
But that's nothing typescript specific.
That's vanilla javascript in the end.
Now Therefore here we could say that here we want to have an object.
And for the key we want to have a string.
Let's say now you're getting an error though because what we don't know here in the end is whether the
object we're getting here really will have that key.
That's the end what typescript is telling us here with this strange error.
For example here I could console lock the result of calling this with let's say an empty object and
then name and here by the way I'm calling this extract and convert because I want to call this without
console log and simply say value colon plus.
But that's just a side note I'm returning a string here and the end with that retrieve data.
But the main problem here is I could call it like this and it would pass in an object as a first parameter
and then a string as a second but correctly typescript tells me that in the end it's not guaranteed
that this key exists in this object now could guarantee us we can again use generic types here.
We could say we got a type T and it will be our object here we can say extends Object but we also got
a type you are a key here let's say.
Which in the end has to be a key of our t type though.
So of this object we want to guarantee that what we get here is a second parameter is a key is a property
of that first type.
And for Dad we can sake extends key of
and now you see the error is gone here and instead of got the error down there because now I'm informing
typescript here that the first parameter should be any kind of object.
And a second parameter should be any kind of key in that object.
And here.
Yes we're passing in any kind of object but we don't have a name key in there.
This error will only go away as soon as I add a name key.
So this is all something we can use generic types for with this key off keyword here also to tell typescript
that we want to ensure that we have this correct structure and that's of course really useful because
it allows us to ensure that we don't make dumb mistakes where we tried to call this function which in
the end would try to access a property that doesn't exist.
So this works but if I tried to access h here I again get an error.
For example let's switch just back to name them for.