Microsoft developed TypeScript, which is a very popular open-source language, and is software developers from all over the world absolutely love this language. This language adds new capabilities to JavaScript and is like a superset of JavaScript. Static type definitions are the most notable addition in TypeSript that are not present in plain JavaScript. You can declare what kind of argument we are expecting and also what type will our function return. Also, we can also see what is the exact shape of the object we are creating. TypeScript is responsible for making our code more secure and robust as it helps us prevent a lot of bugs before even shipping the code. TypeScript catches the bugs during the writing of the code and it integrates beautifully with code editors. Let’s look at an example in this NodeJS with TypeScript tutorial.
Let’s look at the code below
type Person = {
name: string;
age: number;
};
function isAdult(user: Person): boolean {
return user.age >= 21;
}
const waleed: Person= {
name: 'Waleed',
age: 23,
};
const isWaleedAnAdult: boolean = isAdult(waleed);
Code Explanation:-
The first line contains the type keyword which helps us declare our custom type of objects representing persons. After this step, we use the newly created type to create a function “isAdult”. What this function does is that it takes in one argument of type Person and returns a boolean. After this, we create Waleed, which is the example data that will be used to call previously defined data. After this, a variable will be defined to store information about Waleed that whether he is an adult or not. In this example, if we do not comply with declared types, TypeScript will give an alert about an error and will prevent deduce types for us. In TypeScript, it is not necessary to explicitly type everything. TypeScript is ultra-smart and can deduce types for us. For example, in the example code given above isWaleedAnAdult would be of boolean type and we do not need to explicitly type it. Even if we do not declare Waleed as a Person type this would be a valid argument for our function.
Now we have written our TypeScript. Now let’s run it.
The first step is to install TypeScript in our project to use NodeJS with TypeScript. Type the following command in the terminal and hit enter.
npm i -D typescript
Now it can be compiled to JavaScript using the tsc command. Name the file index.ts and paste the above typescript code in it. Use the command given below to run the TypeScript file.
npx tsc example.ts
When you will enter the command given above a new file will be created named index.js. We can run this file using NodeJS.
Output:-
Now we have learned how to compile and run TypeScript. Let’s jump into more details and learn about TypeScript bug-preventing capabilities.
Modify the code we used earlier. After modification, the code will look like this.
type Person = {
name: string;
age: number;
};
function isAdult(user: Person): boolean {
return user.age >= 21;
}
const waleed: Person= {
name: 'Waleed',
age: 'secret!',
};
const isWaleedAnAdult: string = isAdult(waleed, "I shouldn't be here!");
Output:-
This is what TypeScript has to say about the code above. TypeScript successfully stops us from shipping the code that could work unexpectedly.
TypeScript has a lot to offer like interfaces, utility types, classes, and so on. When working with bigger projects TypeScript compiler configuration can be typed in a separate file. You can read more on TypeScript by visiting the official TypeScript docs. This is the official website of TypeScript with official documentation. TypeScript is being on a very large scale in the NodeJS world. NodeJS with TypeScript is being used by many companies, open-source projects, tools, and frameworks. So this is it for today’s tutorial. Visit www.nodejstutor.com for more amazing tutorials on NodeJS. Happy Coding!