So let's continue practicing with these generic types because I know they can be strange to wrap your
head around.
In the end they're really straightforward though.
Let's create an average generic function count and print.
Now this function should take some argument some parameter let's name it element but the name is really
up to you which should be of a generic type.
Because I read don't care about the type at least in certain ways.
So let's say we have one generic type and does function and our parameter is off that type.
That's by the way all the typical for generic types.
If you have a generic function then off your parameter will be of the type doesn't have to be.
But often that is how you work with generic functions as you will see as you get more and more experience
with them.
So here we get count and print with our generic type here and element is of that type.
Now what I want to return here is in the end a tuple with two elements where the first element should
be my element itself and the second element should be a description string maybe rename it count and
describe therefore.
So here I get my description text and here I'll say got no value and I want to use that if we pass in
an element which has no length because my idea with counted describe is that we can pass in an array
or a string and we get back the amount of characters or elements in that string or array.
So if you got an empty string or empty array when he was got no well you.
But here if I check that element dot length is greater than zero then I want to set description text
to got plus element dot length plus elements it could be even more specific and check if the length
is equal to one year and then say got one element else if element length is greater than one we have
description text equal got plus element length elements so that we got the plural form right now here.
Description text to stay for the second element and return here.
Now you'll already see Torchwood complains about the length here because it's not clear that element
has a length.
So maybe we should make it clear that it does.
We could do so by creating a new interface or a custom type with the type.
Keyword here but here I'll go for an interface which I'll name lengthy which in the end is just guaranteeing
that we have an object with a length property which should yield a number in the end.
And then here we can set a constraint and say that T extends and lengthy so we know whatever we get
we'll have a length property and an array or a string would have a length property.
Now year I can console log count and describe and pass in let's say hi there.
Notice we're X because a string has a length property it's not a object it's a string but behind the
scenes javascript kind of converts does to object on the fly you could say and adds a length property
when we need it.
So therefore we can safely call it like this typescript infers that here we return an array with either
a string or type T elements in there.
Now I want to have a tuple so I'll be more specific here and set the return type to be basically an
array with exactly two elements where the first element will be of type T.
And the second element will be of type String.
So now I'm just saying that we return a tuple here where the first element will be well any element
of our generic type.
And the second element will be a string.
Notice that if we safety's this recompile is without errors and prints Hi there got nine elements because
we've got nine characters in their we could all called is within their array where we have sports and
cooking and now you will see that we got got two elements as a description text or all with an empty
array of course.
In which case if I save that we got no value but we won't be able to call this for example with a number
because a number doesn't have a length property and therefore this does not work.
So here again we got our generic function idea is similar to the function before we want to be a bit
unspecific about the type of data we get here.
We don't really care if it's a string if it's an array or anything else which has a length property
we just care about that it does have a length property and then we want to do something with it because
we rely on the length property in our code so we need to guarantee that we get that.
But Abbott and Ed we really have no interest in which type of data we're getting here.
I don't want to lock this down to a string or to an array and I don't want to create a bunch of overloads
to handle all the different possible types.
I also don't want to create a very long union type here because I might forget types which have a length
property or if I create my own object with a length property I might not allow for it.
If I restrict this year to be of type String and array or anything like that so therefore I want to
be more flexible and with generic types we can be that we don't care about the exact type here.
Thanks to the constraint I only care about the fact that it has length property.