In this video Emmanuel Henri demonstrates how to use an interface with your code in Node.js.
- [Instructor] Organizing your code in any application is essential and in this chapter we'll explore a few tools we can use to help us better organize our code and do type shaping. I will start with an interface. Although, we can't really apply in the code of this application, we'll explore how we could use it. Interfaces are only use by the compiler to check for types, and interfaces are literally mean to shape how our data will look like, and then typescript will check the interface types to make sure we're using the proper types upon compiling. So, let me demonstrate with actual code.
So, let's go back to our index.ts and if your server is running, feel free to stop it right now, and let scroll all the way down to where we see the routes, and we'll set our first interface. So, we'll do interface, which we'll call Name, and in this Name interface, we'll shape that we are requiring a firstName which expects a string. So, once we defined our interface, we can start using it. So, what we'll do also is create a function to use our interface.
So, we'll call this function name Creator and in this function we'll pass a name which we expect a Name. So, this is where we can use interfaces. So, you could use an interface for classes, you could use it for anything else. So, what we've done here is pass the interface as what we expect for the type. So, inside of this interface, we have a firstName which expects a string so we could have a last name, we could have a number, and we would pass all these in the parameter.
So, let's go and continue in our function. You could also expect the return type to be of string, in this case so if you want to do something like this, we can do that as well. And let's go ahead and return and do template strings Hello comma and then pass from the name parameter and firstName from the interface. So, if we were using a last name or a number, we could pass these as separate items inside of our template strings or whatever it is your function returning.
So, if you had numbers, you could return a number and have these numbers pass into the function. And then we'll do a comma again because we'll use it in the message we create here to continue the node and express server thing. And then, finish this here. Perfect. Now, let's define an object that will pass the firstName, property and a value. So, let myName equals to firstName and then Manny.
So, I basically created an object here that is called myName and in this object, I have a property firstName which I pass Manny. So, I haven't used the function yet, what I've done here is create a variable to which I'm going to use in the function. And then what we need to do in the console log, I'm going to use the nameCreator, so the function that we just created, so nameCreator, and then pass myName object inside of it. So, what's going to happen here, it's going to use that function, and inside of my argument, I'm passing the object that I created here and it's going to print out this message Hello, my name is Manny.
So, let's save this and let's open our Terminal again, and let's do an npm start, and you can see Hello, Manny, Node and express server is running on port 4,000. So, what we've done here is use the interface to shape what we're expecting, and then here in the function, we expect a name. So, we expect this interface to include the firstName property with an actual value and then we created this property and value inside of an object called myName, and then pass myName inside of the function nameCreator.
So, this is how you would use interface. So, you could use it in any way you want to actually shape how your data looks like.
- TypeScript file conventions
- Using types in Node.js
- Adding types in your functions
- Using TypeScript classes in Node.js
- Using an interface in Node.js
- Using tools to organize your code
- Declaration merging
- Leveraging decorators in your Node.js application