From the course: Advanced TypeScript: Generics and Conditional Types

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Building generic functions

Building generic functions

- [Instructor] Let's take a look at how we can use these generics in combination with functions. So to do this, we're going to replace everything inside of our generics.ts file. We're going to replace this with a log value function and assign a parameter here with a type of string. So now if I console log this, I console log the city and I call the function with the name of the city, then I should be able to compile this correctly. So we'll run NPX TSC, then we'll run node generics and we'll get get Bend. Now, if I tried to pass in some sort of other value, like 600, you'll notice right away there's a problem here with a red squiggly underline. There's a problem here in the problems panel and there'll be a problem if I try to run it. So NPX TSC, we're getting that error because we're passing 600 when we should be passing a string. Now there's a way to make this function more generic with a value type parameter. Let me show you what I mean. If it doesn't matter what type is sent, as…

Contents