Typescript —— 搭建ts的运行环境

工欲善其事必先利其器,我们先安装一下我们ts的运行环境

1.看一下目录
image.png
2.安装npm包
npm install typescript tslint ts-node-dev @types/node --save
3.创建tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs", //代码组织方式
        "moduleResolution": "node", //模块解决策略
        "experimentalDecorators": true, //开启装饰器定义
        "allowSyntheticDefaultImports": true, //允许es6方式 --- import
        "lib": [
            "ES2015"
        ],
        "typeRoots": [
            "./node_modules/@types"
        ], //指定类型从哪里读取
        // "noImplicitAny": false, //不允许有any属性
        // "removeComments": true,
        // "preserveConstEnums": true,
        "sourceMap": true
    },
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}
4.配置脚本命令
{
  "name": "typescript-dev",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "ts-node-dev ./src/index.ts -p tsconfig.json --no-cache",
    "prod": "ts-node-dev ./src/production.ts -p tsconfig.json --no-cache"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^14.14.41",
    "ts-node-dev": "^1.1.6",
    "tslint": "^6.1.3",
    "typescript": "^4.2.4"
  }
}

5.执行npm命令

npm start //就会执行src/index.ts文件

推荐阅读更多精彩内容