Configuring TS + node development environment

Ts direct configuration of the development environment is still very troublesome, and here I summarize the development of a set of templates that can be considered a direct clone this project when in use, with this project-based template https://github.com/fish-node/ template-node-ts to develop on the line.

$ tree
.
├── README.md
├── jest.config.js
├── package.json
├── src
│   ├── index.ts
│   ├── math.ts
│   └── server.ts
├── test
│   ├── math.test.ts
│   └── server.test.ts
├── tsconfig.json
└── tslint.json
复制代码

ts-node

Because js ts is based on existing ones, but the node can not directly run the code ts, the actual use is often need to use tsc ts to compile code into js code, this is very troublesome.

The ts-node package is a node, it can be directly run the code ts, easy to use, it's official repository here github.com/TypeStrong/... , please see its basic usage readme themselves.

I am here is to ts-node writes package.json, everyone after npm install, you can

$ npx ts-node src/index.ts
复制代码

Ts to run the code in this way, it is convenient.

Then we know that there is a node in nodemon, can automatically restart our node in the development program, in ts-node, the correspondence is ts-node-dev

$ npx ts-node-dev src/index.ts
复制代码

Sample Code

In the src directory, I wrote three basic codes ts

  • math.ts is the most basic usage ts
  • Then server.js shows how to write a http server ts
  • index.ts it demonstrates the use of modular ts

Testing Framework

Jest then I use as a testing framework, demonstrated by Jest + Ts write our test program in the test directory. Run the test file, you can use

$ npm run test
复制代码

@types

@ Types / node @ types / jest ts is the type declaration file module which can be given to write js type system, let us enjoy the benefits of the type of system.

Profiles

  • .editorconfig is we configure the IDE to read configuration files
  • tslint.json make our code to check the profile of this document is mainly inherited the official recommended coding style. However, in order to facilitate debugging, the 'no-console' This condition is set to false the

tsconfig.json

Then focus on our ts configuration file.

In general, json file is not supported by the comment, but an official ts For convenience, this file has been strengthened individually, so that we can use annotations, comments, like grammar and js in.

{
    "compilerOptions": {
        "target": "es2018",
        "module": "commonjs",
        "outDir": "./dist/",
        "rootDir": "./src/",
        "strict": true,
        "moduleResolution": "node",
        "esModuleInterop": true,
        "experimentalDecorators": true
    },
    "exclude": ["./test"]
}
复制代码

In which the target is compiled version of js code, because we are node environment, no need to consider browser compatibility issues, so we still try to use an updated version of it.

The module option is our module management mechanism, because our ts-node module management mechanism is not yet pointed es, refer to this problem https://github.com/TypeStrong/ts-node/issues/313#issuecomment- 343 698 812, so we continue to use commonjs specification.

Then rootDir is our source code directory, outDir after our directory is compiled code. At the same time we set up exclude this option, our test file excluded.

Reproduced in: https: //juejin.im/post/5d00bd35e51d45773e418a61

Guess you like

Origin blog.csdn.net/weixin_34130269/article/details/93178980