• Nodejs 使用 TypeScript


    1. 安装依赖
    λ yarn add typescript types/node concurrently nodemon wait-on -D
    
    1. 初始化一个 tsconfig.json
    λ ./node_modules/.bin/tsc --init
    
    1. 编写 tsconfig.json
    {
      "compilerOptions": {
        "target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
        "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
        "outDir": "./dist" /* Redirect output structure to the directory. */,
        "rootDir": "./" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
        "baseUrl": "./",
        "removeComments": true /* 不要向输出发出注释。 */,
        "strict": true /* Enable all strict type-checking options. */,
        "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
        "experimentalDecorators": true /* 为ES7装饰器提供实验支持。 */,
        "emitDecoratorMetadata": true /* 装饰器支持. */
      },
      "exclude": ["node_modules"]
    }
    
    1. 创建 tsconfig.build.json
    {
      "extends": "./tsconfig.json",
      "exclude": ["node_modules", "dist"]
    }
    
    1. 创建 nodemon.json
    {
      "watch": ["dist"],
      "ext": "js",
      "exec": "node dist/index"
    }
    
    1. package.json
    {
      "scripts": {
        "start": "concurrently --handle-input "wait-on ./dist/index.js && nodemon" "tsc -w -p tsconfig.build.json" "
      },
      "devDependencies": {
        "@types/node": "^12.0.7",
        "typescript": "^3.5.1",
        "wait-on": "^3.2.0",
        "concurrently": "^4.1.0",
        "nodemon": "^1.19.1"
      }
    }
    

    执行命令

    λ npm start
    

    添加测试 更多 github

    • 安装jest的vscode插件也行
    1. 安装依赖
    λ npm i -D @types/jest jest ts-jest ts-node
    
    1. 增加scripts
    "scripts": {
       ...
      "test": "jest",
      "coverage": "jest --coverage"
    }
    
    1. 设置VScode的launch.json
    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Jest Current File",
          "skipFiles": [
            "<node_internals>/**"
          ],
          "program": "${workspaceFolder}/node_modules/.bin/jest",
          "args": [
            "${relativeFile}"
          ],
          "console": "integratedTerminal",
          "internalConsoleOptions": "neverOpen",
          "outFiles": [
            "${workspaceFolder}/**/*.js"
          ],
          "windows": {
            "program": "${workspaceFolder}/node_modules/jest/bin/jest"
          }
        }
      ]
    }
    
    1. 设置配置文件jest.config.js
    module.exports = {
      transform: { "^.+\.ts?$": "ts-jest" },
      testEnvironment: "node",
      testRegex: "/test/.*\.(test|spec)?\.(ts|tsx)$",
      moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"]
    };
    
    1. 添加测试脚本 /test/test.test.ts
    it("tobe", () => {
      expect("asd").toBe("asd");
    });
    
    1. 运行测试
    npm t
    

    关于 ts-node

    可以单独的运行ts文件,而无需编译

    λ npm i -g ts-node
    λ ts-node index.ts
    hello ts
    
  • 相关阅读:
    第五课 按键控制文本
    第四课:怎么去掉Activity的标题和邮件图标-20160705
    第三课:控件的使用及按键响应-20160705
    第二课:Android Studo 各文件作用-20160705
    第一课:如何创建Android Studo 工程-20160705
    JTAG各类接口针脚定义及含义
    c# 定时器的实现
    二进制及十进制文件的读写方法
    c# 检测设备改变
    Vue(四)事件和属性
  • 原文地址:https://www.cnblogs.com/ajanuw/p/8933498.html
Copyright © 2020-2023  润新知