带有 TypeScript 的 Node.js
TypeScript is a trendy open-source language maintained and developed by Microsoft. It's loved and used by a lot of software developers around the world.
We can talk about other TypeScript benefits later, let's see some examples now!
Take a look at this code snippet and then we can unpack it together:
The first part (with the
type keyword) is responsible for declaring our custom object type representing users. Later we utilize this newly created type to create function
isAdult that accepts one argument of type
User and returns
boolean. After this, we create
justine, our example data that can be used for calling the previously defined function. Finally, we create a new variable with information on whether
justine is an adult.
There are additional things about this example that you should know. Firstly, if we would not comply with declared types, TypeScript would alarm us that something is wrong and prevent misuse. Secondly, not everything must be typed explicitly - TypeScript is very smart and can deduce types for us. For example, variable
isJustineAnAdult would be of type
boolean even if we didn't type it explicitly or
justine would be valid argument for our function even if we didn't declare this variable as of
Okay, so we have some TypeScript code. Now how do we run it?
First thing to do is to install TypeScript in our project:
tsc command in the terminal. Let's do it!
Assuming that our file is named
example.ts, the command would look like:
npx here stands for Node Package Execute. This tool allows us to run TypeScript's compiler without installing it globally.
This command will result in a new file named
example.js that we can run using Node.js.
Now when we know how to compile and run TypeScript code let's see TypeScript bug-preventing capabilities in action!
This is how we will modify our code:
And this is what TypeScript has to say about this:
As you can see TypeScript successfully prevents us from shipping code that could work unexpectedly. That's wonderful!
TypeScript offers a whole lot of other great mechanisms like interfaces, classes, utility types and so on. Also, on bigger projects you can declare your TypeScript compiler configuration in a separate file and granularly adjust how it works, how strict it is and where it stores compiled files for example. You can read more about all this awesome stuff in the official TypeScript docs.
Some of the other benefits of TypeScript that are worth mentioning are that it can be adopted progressively, it helps making code more readable and understandable and it allows developers to use modern language features while shipping code for older Node.js versions.
TypeScript is well-established in the Node.js world and used by many companies, open-source projects, tools and frameworks. Some of the notable examples of open-source projects using TypeScript are:
- NestJS - robust and fully-featured framework that makes creating scalable and well-architected systems easy and pleasant
- TypeORM - great ORM influenced by other well-known tools from other languages like Hibernate, Doctrine or Entity Framework
- Prisma - next-generation ORM featuring a declarative data model, generated migrations and fully type-safe database queries
- RxJS - widely used library for reactive programming
- AdonisJS - A fully featured web framework with Node.js
- FoalTs - The Elegant Nodejs Framework
And many, many more great projects... Maybe even your next one!