From the course: TypeScript for Node.js Developers
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Use TypeScript classes in Node.js
From the course: TypeScript for Node.js Developers
Use TypeScript classes in Node.js
- [Instructor] If you've worked with ES6 for some time you're familiar with classes, and they're not that different in typescript, except for the added type annotation. So let's create a class that takes our port number and creates a message we can use across our express methods. So the first thing we'll do, we'll create a brand new file in our controllers. So I'll click on the controllers folder, and then click on create new file. I'm going to call this one createMessage.ts. So what we'll do is export right away our class. So we'll create a class. Again if you've used this in ES6 Syntax, in JavaScript, it's very similar. And we'll call this one Messenger. Then, in our class, we'll define a port, first. And define this with annotation of a number. So basically it's going to take a port and the annotation is a number. So the type for this one is a number. Then we'll do a constructor, which we'll build with the port, pass in to it. And then we'll define this.port = port for this class…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.