Generics Tutorial in Typescript

Posted in Uncategorized

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

In this Generics tutorial in Typescript, consider the following logger function that takes type of “any” …

No question this works. But can we do better than to use the “any” type.

Here we use the generic type variable T …

This variable T takes a type which we give to it in line 8 and 9 as type “string” or “number”.

More commonly, we leave that part out, because the compiler can infer the type from the argument type. It knows that “greeting” is a string and “answer” is a “number”. So we simply write …


Related Posts

Tags

Share This