TypeScript configuration: How to use TypeScript with Node.js

introduction

In front-end development, with the continuous popularity of TypeScript, more and more developers use TypeScript for development. However, using TypeScript in Node.js is relatively uncommon in backend development. This article will introduce how to configure TypeScript in Node.js, and how to use TypeScript to develop high-quality applications.

Install TypeScript

  • Before we can start using TypeScript, we need to install TypeScript first. It can be installed through npm, just execute the following command:
    npm install -g typescript
    
  • After the installation is complete, you can use tsc -vthe command to verify that TypeScript is installed correctly.

Initialize the project

  • Initialize a Node.js project:
    npm init -y
    

Configure TypeScript

  • Create a file in the project root directory tsconfig.jsonand configure it as follows:
    {
          
          
        "compilerOptions": {
          
          
            "target": "es6",
            "module": "commonjs",
            "sourceMap": true
        },
        "include": ["src/**/*"]
    }
    
  • The above configuration specifies some options of the TypeScript compiler, such as compiling target as ES6, module type as commonjs, generating sourceMap, etc. At the same time, the path of the compiled file is configured.
  • You can also execute the following command to create the file
    tsc --init
    
  • After the execution is complete, a file will be generated in the root directory of the project tsconfig.json. This file contains configuration information for the TypeScript compiler. It can be modified according to actual needs.

Write code

  • After creating a TypeScript project, it's time to start writing TypeScript code. In TypeScript, you can write code using ES6 or higher syntax.
  • When using TypeScript in a Node.js application, we need to use Node.js' built-in module system. TypeScript supports CommonJSa module system, which means we can use requireand module.exportsstatements to manage modules. Here is a simple example:
    // src/app.ts
    import {
          
           helloWorld } from './hello-world';
    
    console.log(helloWorld());
    
    // src/hello-world.ts
    export function helloWorld(): string {
          
          
        return 'Hello World!';
    }
    
  • In this example, we create two modules. hello-world.tsThe function in the file helloWorldoutputs a string. For app.tsthe file, we use importthe statement to helloWorldimport the function and output its return value in the console.
  • srcCreate a file in the directory index.ts:
    export function sayHello(name: string) {
          
          
        console.log(`Hello, ${
            
            name}!`);
    }
    
    sayHello('Tom');
    

compile code

  • The above code exports a sayHellofunction and passes a parameter when the function is called. Enter the following command in the terminal to compile:
    tsc
    
  • After compilation, srca new index.jsfile is generated in the directory:
    "use strict";
    Object.defineProperty(exports, "__esModule", {
          
          
        value: true
    });
    exports.sayHello = void 0; function sayHello(name) {
          
          
        console.log(`Hello, ${
            
            name}!`);
    }
    exports.sayHello = sayHello;
    sayHello('Tom');
    

run code

  • Finally, we can run the generated JavaScript file using Node.js:
    node src/index.js
    
  • The terminal output Hello, Tom!shows that we have successfully developed using TypeScript in Node.js.

Summarize

In this article, we introduced how to configure TypeScript in Node.js, and demonstrated how to use TypeScript for back-end development through code. The strong type feature of TypeScript can ensure the readability and maintainability of the code. If you want to experience the superiority of TypeScript in back-end development, you can also configure it according to the above steps. It should be noted that the configuration information of the TypeScript compiler needs to be modified according to actual needs in order to achieve the best development effect.

Guess you like

Origin blog.csdn.net/McapricornZ/article/details/131395490