December 2017
Beginner
372 pages
10h 32m
English
In the previous example, we can assign any type as a type parameter and, then, use that type to call the functions. This does provide us with flexibility but it also opens the door to various scenarios where our code may not behave consistently.
For example, if we add a new function getSpecificItem to our GenericClass which takes-in a string and then returns a value associated with that string as shown here:
getSpecificItem (title: string): T{ for(let value of this.items){ if(value.title == title){ return value; } } }
In this case, the preceding function, when added to our class, will highlight an error that the title property does not exist on the type T. This is because T can be of any type and not all types have the ...
Read now
Unlock full access