From the course: Scaling TypeScript for Enterprise Developers

Unlock this course with a free trial

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

Creating a generic type

Creating a generic type

- [Presenter] Let's experiment with creating a generic function. Let's replace our code that's in the Script.ts file and this function is going to be called printCity and here we'll add our generic type, which says that city is of type. We then will use the void keyword here, and the reason that we're using the void keyword is because this function is going to console log something, it's not going to return anything just yet. Then when we want to call the printCity function, we can then specify what value we want to send to this function. So the first time we'll send it a string. The second time we'll send it a number. So "printCity<number>", excellent. So now if we try to run this after we save it, we want to make sure we're in the right folder, of course, and then we'll run "node script". We should receive those values back. If we happen to replace this with the wrong type, we would have a compiler error and then we wouldn't be able to compile that. So that is how we create a…

Contents