you are watching typescript Master Class and in this video we are diving into the world of generic a powerful feature that allows you to write Dynamic and reusable code while still maintaining the type safety of typescript so whether you are building data structure like Stacks or Q or working with apis or creating utility function generics makes our life easy by letting us handle multiple types on that particular single piece of code so by the end of this video you will not only understand what generics are but also learn how to use them in functions classes
and as well as in interface we will explore real world examples and break down few Advanced topics into understandable junks and as always you will find all the GitHub repository Link in the description so let's roll the intro and start to unlock the full potential of genx in typescript so now let us start by taking a very simple example I want to write a function that takes an array and it will return its first item okay so what I will do I will say function then get first array element then it will take array and
it is going to be of type number number array right and of course it is going to return a number and here what I will do I will say return array of zero so what this function is basically doing it is taking an array and returning its first element so how to use this function so I'll create a constant of say number array number array and I will say say for example 1 2 3 4 and 5 and here I will say call first number array item equals to get first array element and I will
pass the number array symol so if you see here this first number array item is going to be of type number why because this get first array element takes an array and returns a number so this is working perfectly fine now my requirement has changed and now I want to write a function that takes a string array and return its first element so here I will say con string array equals to and I will say for example apple and let me add one more fruit banana and say guava so I have a string array and
I want to say con first string AR array item equals to now what I have to do see this function is taking an array of type number an array of type number but now I want to pass an array of type string so if I pass this string to this function it will obviously throw error right because you cannot pass array of string to array of numbers so what choice I have so one choice that I have is to copy this function okay then what I say get first array element and I want to name
it as string and then I will change it to string array and of course this will return a string and here I will take this function and use this instead great so this will work now again say for example there is a array of object now again if I have to get the first item from the array of objects what I will do I will write another function right I want to pass an array of Boolean so for example so I want to write a method that get me the first array element of a Boolean
so again I will write a method for that so if you see this is not the ideal way right you cannot write the same function the same logic for different data type this is exactly where gener comes into picture so here I remove this function right and what I will do here I will say this function is going to take a generic type that means at run time this generic will give me the actual array type that I want to pass okay so here I want to pass number then later on I might want to
pass string right at some point I might want to pass Boolean right so this is where generic comes in and it will take a type okay so I will say array type okay and this array type I will use as this arror type okay and this arror type will be returned from here now what does this mean so when calling this function this function expects the data type to be passed so if I say here as number okay this number will come as arror type and this arror type will be used here as the argument
that means when I say number it will become number and this array will become type a number of array and it will return a number okay now I will use this function again and instead of number I can say string now this string will come as this array type okay and this array will become string type AR it will return a string so now this same function is working for number as well as working for string now can I make it work for Boolean so what I will say con first Boolean array item equals to
get first array item and I will say Boolean and I will pass a buan array so true or false and most importantly if you see here what will be the return return type so if you see here first number array item return type is number if you see first string array item the return type is string and first Boolean array item what is the return type the return type is Boolean and how this is possible because I'm saying Boolean string and number as this generic parameter and this generic parameter is what is being return so
if I say number it is returning a number if I say string it is returning a string if I say Boolean this will return a Boolean okay so this is the whole idea of generic you pass a type on which which you want your function to be typed on so if you want your function to be typed as a number array you pass number if you want it to be a string array you can pass it as string and the general convention is to use a single letter type so say for example I can say
t okay and here I replace this with t and here also I can replace this with t this is just a convention okay so if you want you can name anything a b c if you want you can write your own name okay so just how I said a type right but this is just a convention Tu u v X like that okay so the same function we can use it with number we can use it with string we can use it with Boolean we can use it with any data type and any data type
means there are some ways in which we can do constraints that means we should be able to use certain type of data but we'll see that later in the video for now we can pass any data type and make it work okay so we'll take one more example so here I want to create a function that creates a tle for me so a typescript tle so this will take two arguments so let me write the function first so function create double and it will take the first item say for example it is of type string
and second item and say for example it is of type number and what this will return return this will return string and number doule and what this will do simply return first and second okay now what I want to do I will say con string number tle equals to create tle okay and first parameter what I have I have to send I'll say apple and the number number I'll say four okay so this will create a tle and what will be the type of this one string comma number great now what I want to do
I want to create another tle string bullan topple equals to create topple and I want to say say for example banana and I want to say false so this is the tle that I want to create but this won't allow me right because the first argument has to be a string the second argument has to be a number so first argument I'm passing it a string but when I try to pass the second argument as Boolean it is immediately throwing error right so here also we are going to use generic so generic has come to
rescue so what we will say see here there was only one argument so we used only one generic type here there are two different arguments one is a string type and one is a number type so here I take two generic parameters T and U of course name you can change right so so the first one instead of string I will say t and the second one instead of number I will say U and what this is going to return this is going to return T and U instead of string and number so if you
see here now the error is gone why it is gone because it is now inferred that the first parameter of type T is a string and the second type is number and Boolean here so here I'm not explicitly saying but I can pass the explicit generic parameter as string and number here and here I can say string and Boolean right so if you see the type here it is still string and number but if you see the type here it is going to be string and Boolean right so now for example I want to con
I want to create a string string tle okay here what I will say create tle of type string comma string and here what value I have to pass I'll say guav H and can I say four so if you see it is giving squiggly why it is giving squiggly because this parameter clearly says this this argument has to be of type string so this parameter I can say say for example available so it has to be of type string it cannot be a buan it cannot be a number it cannot be of any type only
it can be of type string because here I'm saying string okay so we can have one parameter like here one generic argument we can have two generic arguments we can have any number of generic arguments as much as we need and what this is helping us in achieving in the same function we can change the data type at runtime that is the important here so this is the usage of generic in a function now let us check how we can use generic in a class so let us write the good old stack class so I
will say class stack okay and here I want to instantiate an array so I'll say private items and this is going to to be of type number array and I'm initializing it with an empty empty array then I want to have a push so what this push is going to take this push is going to take a value item of type number and here what I can say this do items do push and I will pass the item then I'll write one more method say pop okay and what this will return this will return the
number that it has just popped so what this will return this will return number okay okay and it will say return this do items do pop and if you see we are getting a squiggly here why we are getting squiggly because this pop might return a number or it might return undefined if the array is empty itself so we cannot pop anything out of this one so here I will say number or undefined so this will fix the error now here what I can say I can say con a number stack equals to new stack
then what I can say I can say number stack do push say for example five okay and I will say number stack dot say push and I will say push 10 then number Tag do pop and let me assign it to a variable okay so con uh let me say number pop equals to number stack and pop and uh it is giving spelling error so let us say p PP popped okay anyway spelling doesn't matter but but anyways let us fix it then what if so this is working with number number right so can I
say con string tag equals to new tag and here what I want to do I want to push string so I will say string stack okay do push and say for example apple is this going to work no right why because this stack it item type is of type number it is expecting an item of type number and it is popping a type of number so here the number type is hardcoded so when I create number stack it work but when I'm creating string stack it will not work right so how to fix this one
of course gendex will come into rescue so I will say t so what is this t t is going to be the items type it could be number it could be string it could be Boolean it could be a it could be a object type right now here T instead of number I will say this is of T array it could be number array it could be string array whatever I pass when I'm creating this when I'm instantiating the stack class so and since this is T this is going to accept an item of type
T and it is going to return a item of type t or undefined so now this stack is not hardcoded as a number stack but a stack of type T which at run time can be anything so if you see here I can pass number so of course all the number will work and at run time if I pass string now if you see I'm able to push string and if you see this pop type what is the type of this pop number and undefined now here if I say string pop equals to string stack
do pop if you see what is the return type of this one return type is string or undef if you see here this is number or undefined now how this is working because of this t or undefined and this T is whatever we are passing here and what we are passing either string or number so that's how with the same stack class I can use it as a string as a number maybe I can use it as a Boolean maybe I can use it of a particular Class Type now here we are passing this number
or this string right now say for example in this string I'm passing this as a string type now even if I don't pass this one is this going to work yes so this is going to work and it is taking this apple as a string type but what is the problem here so if you see this return type it says unknown it doesn't know what it is going to return okay so here we will say string now it knows what is going to return so it returns string / undefined so even if you remove it
will not throw any error here but as such you will lose all the typescript type safety okay what do I mean by that so say for example this is a string this is going to be a string so here I can say string pop string pop Dot length now why I'm getting length as the property because typescript knows this this string popped is of type string right now if I omit this one string okay as such you will not get any error okay but if you see this string properties of type unknown so if I
say string pop if I say string pop and if I say dot I am not getting any suggestion why because it doesn't know what type this string pop is okay so you always explicitly specify that what type this generic argument is going to take then only we will be able to leverage the full Power of typescript Now if you see I'm getting suggestion which is specific to string okay now here if you see this number pop so if I do number pop and Dot here what I'm getting I'm getting the typescript context specific to number
right so that's how we can Leverage The Power of typescript with generic just because I'm using generics I'm not losing the type safety as long as I'm writing the generics in a proper way and I'm explicitly passing the generic type when instantiating the object or I'm using it in the function so we have seen generics in function and in class where else we can use it we can use it in interface so let us so let us check one example for interface so what example we are going to cover so if you see orms like
type orm micro orm they expose a repository and that repository we can pass a particular entity type and when we pass that particular entity type we get say for example find find one that is specific to that particular entity so let us try to create a similar or type of repository pattern of course we are not going to write the whole repository it is just the class and the interface okay so here what I want to do I want to create an interface okay and I'm going to add two methods one is to add an
item and of what type what type okay so say for example here I want to define a user interface okay so I want or it could be a class also right so I'll say interface user and for our example we'll just work with interface okay so I'll say ID of type string okay then name of type string and say for example email or of type string okay so this is the user entity okay and here I'm creating an interface okay that has that has a add method that takes a user type okay and what it
is going to return it is going to return wi and here I have to give a name so that's why just throwing this all this quickly so I'll say repository okay then so it has a add method it has uh say get all method so it won't take any argument but what this is going to return this is going to return user array okay and say for example let me add a delete okay and what this is going to take this is going to take an ID ID of type what id of type string and
what this will return either it can return a void okay or it can return the user that it deleted okay so in our example let us take the user that it is deleting okay then what I want to do I want to create a class okay and I want to call it as o repository that Implement okay implement the repository interface okay now if you see it is giving Squigly why it is giving Squigly because we have not implemented all the methods that are given in the repository interface so what I will do I'll say
Quick Fix and Implement interface reposit so it has just did the implementation and it is throwing a error method not implemented and anyways for for our purpose I'm not going to implement this method because implementing this me method is not as important as understanding the in and around of what is happening right so for our purpose at least I'm not implementing it but we'll see now what has happened here if I create a say for example con const user repository and I will say new omm reposit okay then I can say user repository dot say
for example get all okay so if you see this one I say const all users okay so what will be the type of all users going to be a type of user array okay now this orm repository now can only work with this user type what if what if I have another type say for example product okay I have a product type okay this has say for example a product ID of type string okay and say for example product name of type string and say for example say for example I have product description okay and
this is also going to be of typ string now what I want to do I want to write I want to use this orm repository with this product interface as well so for this what I have to do I have to write another repository that take instead of user product okay then I have to write another orm repository that implements the product repository and that is going to be inefficient right because here if you see in our example we have two product but two interface but in real world we might have multiple interface right so
now let us convert this to generic how so instead of this user it is going to take the argument here in the generic as a generic argument and here instead of D instead of user I will say it will take a item of type T it will return an item of T array and when deleting it is going to return a type T CR so now this is a generic reposit now it is not bound to only user right so so here if you see it is giving squiggly why it is giving squiggly because this
depository expects a argument of type T so I'll say t then what this orm repository item it is going to take is it going to take user no right it is going to take type T and it is going to return T of array and here also it is going to return T now if you see we are getting Squigly because it doesn't know what is T so who will Define what is T here so it it implements repository T and this omm repository will take this t as the argument or its generic argument right
so here this T whatever I'm passing this T will be part of this repository and this repository is being implemented here so since this is of type T it is going to take an argument of type D get all will return an array of type T array and delete will return this of type T now here if you see this generic argument what it is taking it is not taking anything so let me send user so if you see here now this all user is going to be have type user array but now I will
say con product repos repository new omm repository and I pass this generic argument as product okay now what has happened here this orm repository is taking product so this T is Now product or it can be user so this T is being represented here in the repository as T so now it can be either user or product or can be anything right any other inter or any other class right and this repository T is defining what this ad is going to take what this ad is going to take what this get all is going to
return and what the delete is going to so if you see here say for example G deleted deleted product okay equals to product repository do delete so if you see and it is giving squiggly why because it expects one argument so let me pass four okay some parameter okay as such this parameter doesn't matter but what I'm more concerned about is it's delete return type so if you see here what it is returning it is returning product okay here get all what it return user why because I'm passing the generic argument as user right here
I'm passing the generic argument as product so whatever I'm passing here this repository is implementing that particular data type here I'm saying T so user has become so here T has become user right T has become array of user return type is T that means user so if you see here how useful this generic argument has become so whatever type I will pass it will return me that type right and it comes with what type safety so if you see all users all users is of type users so I can say all users say for
example find and I will say user and here if you see what I'm getting email ID and name from where this is coming this is coming from this user ID name and email because it clearly understands what is this user type if I do not pass this user argument okay and if I try to do the same thing if I try to do the same thing here user if I say dot I'm not getting any suggest why because it doesn't know what is the user type but if I pass this generic argument as user right
now it knows what is the user type and I'll get suggestion email ID and name right so see how powerful this framework has become and how useful this generic is now so I can say email right so with this help of this generic parameter we can manipulate the same function to do multiple things to do multiple returns right and based on the type we can change the logic by using type guard so if you see how powerful this framework is becoming and just imagine the massive scale in which we can write helper function so this
is the just a basic example and next we'll check different types of constraint now if you see I'm able to pass user I'm able to pass product and if you see here I'm able to pass string I'm able to pass number whatever I want I can pass right but there are certain scenarios in which we want to restrict what we want to pass as the generic that's what is called as generic con right so we'll check the generic constant next now let us start discussing constants so what are constants so this generic type it can
be anything as of now so say for example this get first are element so here I'm saying number here I'm saying string also I can say Boolean that means I can pass anything right now what I want is I don't want to this T to be anything but it should only be of type number or strict I don't want it to be of type Boolean or to be a type of set or to be a type of any other map or any other object I want it to be only number and string so how can
I do that one so I can say t extends than number or string see as soon as I do this one I'm getting squiggly here in the Boolean and what problem it is saying it is saying type Boolean does not satisfy the constraint string or number so what I have done I have constrained this type T to either of type number or string it cannot be Boolean or it cannot be anything else so this is the first example of doing a data type constraint so what I will do I will remove all of this we
will start learning this constant from scratch so this is the first example so here bullan you cannot use why because I'm constraining it to number or string say for example if I say Boolean also so this will start working say for example I remove string so this won't work so this is the very basic example of doing a constant so I will keep this as string and I will keep this as comment so this is the basic example so here what I'm doing I'm resting it to a particular type now I want to write a
function that says GRE so it will say hello user and it takes a t and here there is a person object it is of type D and here I want to return say for example hello now here I want to say hello and say result oneals to greet and it is going to take a person and say for example ID 1 and say for example name name let me say Alis so this is what this is taking so this grid function is taking ID and name as person of type T so here in the generic
what I can say this generic is taking an object which is of ID of type number and name of type string great so if I run this one I will get a string return return type of course right now say for example const hello result 2 equals to greet and here ID of course of type number and say for example email of type string then here I will call this as say ID of type 2 and email so let us say for example Bob at test.com so this is what I'm passing and this is also
going to return the string now what I want is see if you see here ID is common but name and email one has name and one has email now instead of saying hello I want to say hello and username so I'll say person dot dot what that is the question here I'm not getting name or email and why I'm not getting because this type T doesn't know what it is going to be of type is it going to be of type buan is it going to be of type uh set or is it going to
be a type object but if it is of type object is it guaranteed that it is going to be a property name so say for example name it might have a name it might not have a name so if you see first example we have a name property but if you see the second example it doesn't have a name it has a email property but we are not getting any error here where we are exactly getting the error here in the function because it doesn't know whether the person which is of type D has a
property name it might exist it might not exist we don't know so we have to tell types script that this T must have a property name for this function to work because I'm returning a string that is going to use this name property from person but typescript as of now doesn't know so how can I make it aware so I can say extend an object which has a property of name String so if you now see this error is gone why because this T extends an object that must contain a name so now for sure
this person has a property name if you if I say dot you'll see this suggestion so I can say name but now where it started throwing error it is throwing error on hello result 2 why because this doesn't satisfy the generic constraint on T why this doesn't have name instead it has email so this type of ID and email we cannot use on grate because this doesn't satisfy the constant that we have added on that grd method what constant that it must have name see it can have other properties like it has ID it can
have say for example nickname which is of type string and here also we have to pass a nickname say for example Lis but it must contain name if it doesn't contain name it will throw error it must contain name along with other properties that is acceptable but it must contain name that's what this extend means now for sure I can use person. name because always there is a guarantee that it will have name otherwise it will throw compilation error itself right so if you see here what it is saying ID and email doesn't satisfy the
constant name so name is missing so as long as it has name it is happy if it doesn't have a name it will throw compilation error so that's the power of generics with constraints now I can manipulate this G to be to must contain a name property otherwise I'll not accept it so this is example of another generic constant that uses object now now can I write a generic function that accepts only function so let us check that constant okay so here what I'm saying I'll write a function okay which is call function and return
result this is going going to take a function of type I don't know we'll write what is the type but this has to be a function so that so that what I can say return FN and I'll call the function here and if you see I'm missing and if you see the function name is not correct so I'll say function so I'm calling this function here I'll pass a generic type D and here I'll say this is the T now what is the guarantee that that this T is a function so here I can say
con result 1 equals to call function by result and I'll pass four see I'm not getting any squiggly here on line number 25 I'm happily calling the function call function and and it should be return result okay return result call function and return result I'm passing four but here I'm executing this so what does this mean what I am doing I'm doing four like this this doesn't make any sense so that's why it is giving Squigly on line number 22 because this T is not guaranteed to be a callable function it could be a integer
it could be a Boolean what stops me nothing so here I have to constant this to be of type function so I'll say extends I'll say extends a function now what does this extend it extends a function that doesn't take any argument and it can return anything so so let me write a function function say for example say hello it doesn't take anything and it doesn't return anything and and I will pass say hello now this will work right this is working because this T extends a function that doesn't take any argument and it returns
anything so so for example it can return number so say for example six and I will say it returns a number this is going to work of course this is going to work right because that's the constant that I have added now can I pass true no because it will throw error because it expects a function so I will say say hello now what if what if there is a parameter here so say item of type number now if you see we are getting squiggly why because this expects uh function that doesn't take any argument
but here it is taking one argument so what I can say I can say do ARS of type any array that means this can take any number of arguments it could be zero also so now this won't throw any error if this argument list is zero and if argument list is one then also it won't throw any error and let it return this item so what does this extend means this extend means a function now if you see here there is a any type that means it can return anything now what if what if I
want to pass the return type as a generic parameter so so we already have D so we already have t I'll pass U as another generic parameter and this is what is going to be the return type and since this is the return type what I will do this function is being called and being returned so what this will return this will return type U and here let me call this as f f stands for function and r r stands for return type so R this is FN is of type f and this is going
to return Type R great so what this this mean this function is going to return a Type R now here what I have to do I will say what is the generic type here generic type here is of type say for example let us take this say hello so what this parameter is taking this is taking one item of type number so I will say Arc Z of type number and this is going to return number and what will be the second R type number so what is this generic argument here so if you see
one is f which is of type function so this is the F parameter so this F must be a function that takes one argument of type number and it returns a number type now say for example I'm saying it is going to return string type and here I'm saying string type now can I pass say hello why not why it is throwing this quickly because according to this generic parameter it is returning string type but if you see this hello it is returning number type so if I say string here and if I say string
here now this will work and what else I have to change I have to change the parameter so I'll change the parameter now this will work so that means I can write generic that takes a particular function type of a particular set of argument and return type and it will execute that on my behalf and return me the result so if you see here what is the result one it is of type string what if I say for example Boolean and it will return Boolean now how I have to change this generic so this argument
is taking type Boolean and what this is returning this is returning Boolean and here also I'll say bullan as the other generic parameter now what will be this say hello say hello res will be Boolean so this F has a constraint that it can take only function and what is the function it can take any number of argument and it must return Type R R is being passed here as Boolean so this is the first generic parameter that is a function of type one Boolean argument and one Boolean result and this Boolean we are passing
it here because if you see this R is this R and it returns this R so Boolean so this function returns Boolean which is being passed as the second generic argument and the same thing of Type R is being returned right so this is a constraint in which I'm saying it could be a function of any particular type I can manipulate it accordingly we have seen different types of constraint right could be a literal constraint it could be object constant or it could be a function constant now let us write a constraint in which the
generic argument can take only of type class this is important because this same fundamental concept we have used when we are writing class based decorator so if you have not seen that video feel free to watch that because there we have discussed this in extreme detail so let me write a function that will create an instance create instance and it takes a generic type okay and here I can say say for example class type of type D and what this will return this will return new class type so what does that means whatever I'm passing
it will get an instance of that and it will return me so of course this is giving squiggly because types script doesn't know if this T is a Class Type so that it can instantiate it what I can do I can say say for example class user okay and it has a function get user and this returns return this dot return say for example Bob now I can call this function I'll say const instance equals to create instance and I can pass user and let me correct this spelling or it's possible that I can call
this with five so this create instance on line 41 is not throwing error I can pass anything but who is throwing error on line number 32 we are getting error because this T could be anything now my my first Target is to restrict it to a type of class it can send only class nothing El else so how can I do that so I'll say it extends something now what is that so how can I define a class so I can say new because I instantiate class with this new keyword and it is of type
instance type of T and this is the Constructor so Constructor can also take some parameters so I'll say any so what does this mean now this means it can take anything that has this new instance cable keyword or this Constructor type right what this is going to return this is going to return instance type of T right now this constant means a class it can take only class not the object of the class not a function not a Boolean type or integer type type or object type but only a class so if you see we
are getting squiggly here in five so what if I pass this user now this will work now it is not throwing any error and if you see this instance it is of type user and how can I say it is of type user I can say instance do if you see I'm getting the typescript or the vs code suggestion get user where this is coming from here right now so for example I have a class person okay and say get person and say for example return false this is returning false so if I say create
instance and pass person if you see now what suggestion I will get I'll say get person and get person and if I store it in a variable say for example person type okay so here what will be the person type it is going to be of type Boolean because this create instance exactly knows what it is taking as an argument and what it is returning as a return return type so here I can say I can explicitly say person and if I want I can pass the Constructor WR a Constructor and and and if you
see here it should be any of array not simple any so that it can take one or zero arguments or more than one arguments and it can be zero also so this is the example of constraint of Class Type so that means I'm restricting it to be only of a class now let us check the next example so here say for example I have a type of course and what is the type of course so this is going to be of type say for example I have a property called as course name of string and
duration of type number so this is my course and now what I will do I will say const course of type course and I will say course name equals to typescript and duration say four four months or four weeks whatever it is then what I want to do I want to write a function I want to write a function say get property and this is going to take key of type not sure we will add it and this is going to return something we don't know what this is going to return and what I'm trying
to say I'm saying return say for example course of type key so here in get property I should be able to pass course name so it will return typescript I should be able to pass duration so it should return me four that is what I want to achieve so I want to say course name equals to get property and I want to pass course name course name so here I'm expecting it should return me the course name property it should return it should return the course name property value so here I'm expecting typescript that is
what I want to achieve now here I want to pass it as a generic constraint so I will say t so key of type T and here if you still see we are getting error so I'll pass this t as the generic argument right now if you see we are still getting error why because there is a possibility that instead of course name and duration I might send say for example ID get me course ID but is there a course ID do we have ID property in course no it has only course name and duration
but what stops me from passing ID nothing so now I have to restrict it it restricted to what restricted to either course or duration so that I should not be able to pass ID otherwise how typescript will know what property it has to return return so what I can say so I will say t extends extends what key of course now what does this mean what are the key of type course it is course name and duration so this D can be only course name and duration it cannot be anything else so if you see
here ID I'm getting Squigly why because this ID is not key of course what is the key of course it is either course name or duration if you see course name is of type string what if I pass duration duration is of type number so let me also rename this do1 duration right so this is going to be of type number how does this know what data type it is because it clearly knows that t which is either duration or course duration is key of course right it cannot be anything else it cannot be of
ID or it cannot be of any other type so if I say duration it knows it is a key of course and duration is of type number so it return type is number and it will return four in this case right that is not important but what is important here is what data type it is returning this is going to be of type number number right now say for example say for example there is a type data and it has a info of type string now this get property is only working with course can I
write one more generic argument can I pass one more generic argument to this so that it can work with any data type or any object type or any type so here I'm saying data and it has a property of info and I will say const data equals to and this is going to be of type data and say info and let me say some information and here it is not date it is Data okay now here this get property can I pass info no why not because info doesn't exist on course because I'm here hard
coding it as course so here instead of hard coding it as course let me make it as another generic parameter U and I will pass this U now what does this mean this means T extends key of U and there is a u so here what I have to do I have to pass what I have to pass I have to pass info info and also I have to pass what data type this property is going to come from so I will say data now if you see I'm not getting any squiggly then but I'm
getting squiggly on line number 59 because I'm saying it is course so here I will say object of Type U and here I have to say object of key so then it is giving squiggly why because it expects another parameter I will say data so here I'm passing info and data and I'm trying to get the property out of that data so here I will say info data right now what if I want to use this with this old course so I'll say const course duration equals to get property now what property I want to
get I want to get say for example course name from where from the type of course and here I will say course name so if you see I'm getting what I'm getting suggestion so as soon as I do this it is saying course name and from what from course so if you see this duration so spelling I will fix this one so if you see what is the type string because I'm saying course name so I'll just change this name instead of course duration to course name it is string now if I want to get
duration what I have to do I have to take the duration property from course and if you see I'm getting squiggly so what value it is expecting is expecting duration property right duration from course if I do this what value I will get I'll get course duration as number so now see how powerful this generics has become so with one function I can constant it to a particular type and I'm getting that particular suggestion not only the suggestion but also I'm getting return type of that particular type so if it is saying duration I'm getting
number if it is saying course I'm getting string so if you see if you properly use generics then we can get type safety guarantee here and we can pass different data type based on our needs of course this might look silly because these are made of examples but in real world if you restrict this kind of data type you will not only get the proper return return type but also type safety in your function arguments that you are sending then next example so here I want to say function and say for example I want to
say display size and it takes a property of T and size size of type D and it is doing something with the function that is not important now what is important here is this type D can be of anything now but this size I want to restrict it to small medium and large nothing other than that so I'll say extends extends small or or medium or medium or large right now when I call this function display size here I'm getting suggestion what suggestion I'm getting large medium and small right if I try to pass Xcel
or extra large is it going to allow no why it will not allow because this parameter which is of type T extends little type of small medium and large nothing else so you can only pass small medium and large you cannot pass anything else you cannot pass true right you cannot pass four or you cannot pass say for example this class person you cannot pass person here you can only pass small medium and large so this is example of conting to few literal types say for example it could be a Direction so I can say
east west north south only you can send east west north south you cannot send anything else so this is example of extending literal types then next example that we'll check is implements a particular interface that means we have already seen that it takes a type of class now what I want to do is I want to write a function that takes a object of a class that implements a particular type of interface this might be confusing so let us check with a code example so here I have a interface interface has get has get name
and here I'm passing get name as a function and Returns the string then what I want to do I want to create a function that print name of on generic type T and it will take an object of type T and of course it is going to return a string then return object dot I don't know what is the dot here but eventually I want to use get name like this this thring error I'm not getting any typescript suggestion that means what I have to do this T what is this T actually this is a
object of a class so here I will write a class say for example animal okay animal and here what I want to do say for example dog equals to new animal here I want to pass or in this print name I want to pass this dog but this dog doesn't have a property get name because if you see I'm getting squiggly on line number 75 not on line number 81 that means nothing stops me from passing dog as a object type of animal to print name but this animal doesn't have the get name function implemented
so that means it will immediately throw error where on line number 75 because this dog is of type T Dog object of type animal so T is animal here and dog is a object of type animal but it doesn't have this get name method implemented but I have this has get name interface right so what if what if I say implements has get name now if you see animal is incorrectly implemented so I'll say Quick Fix and get name now this animal has a get name and I'm passing dog which is of type animal but
still it is throwing error because this T doesn't know that it is of type animal that implements has get name so here what I can say extends simpol extends has get name so any class that implements has get name interface its object will be accepted as t otherwise not right that means I cannot pass any random value it must Implement that interface and it must Implement that method of course because once you implement it will force you to implement this method otherwise it will throw compilation error so here what I'm seeing this D must extend
this has get name interface so this is what this animal is doing and all the object of this type animal will be accepted in this printer name so what we have done we have restricted it to a class objects that implements a particular interface so this is all about generic constraint generic itself is in a powerful feature and on top of that if you use generic constraint it even becomes more powerful so I really hope that you were able to visualize the power of generic and the power that it gives us to the typescript developer
to write libraries and other utility function that uses generics and and also I hope you learned how to use the generic method so with this I'm concluding the generics topic and if you have any suggestion any comment feel free to put it in the comment section do like share and subscribe to the channel because I keep bringing this kind of content and I will see you in the next video [Music]