Typescript環境建制(後端 NodeJS)

  1. typescripts 環境建制教學
    1. 配置 tsconfig
    2. 把這行加到 tsconfig.json:
    3. 最後 tsconfig.json 會長這樣:
    4. package.json 增加:
  2. 完成~

官網 Typescipt

typescripts 環境建制教學

npm install -D typescript tsx @types/node
npx tsc -v  //檢查ts 版本

配置 tsconfig

官網配置
npm bases 配置

A base TSConfig for working with Node 20.
Add the package to your “devDependencies”:

npx tsc --init  // 初始化 tsconfig
npm install -D @tsconfig/node20

把這行加到 tsconfig.json:

"extends": "@tsconfig/node20/tsconfig.json"

最後 tsconfig.json 會長這樣:

{
  "extends": "@tsconfig/node20/tsconfig.json",
  "compilerOptions": {
    "outDir": "dist",
    "allowJs": true,
    "rootDir": "src"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

package.json 增加:

"scripts": {
    "start": "cross-env NODE_ENV=development node example.js",
    "build": "tsc ",
    "dev": "cross-env NODE_ENV=development tsx --watch ./src/index.ts",
    "watch": "npx tsc --watch --noEmit ./src/index.ts"
  },

完成~


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以邮件至 kimfei2014@gmail.com
github