typescripts 環境建制教學
npm install -D typescript tsx @types/node
npx tsc -v //檢查ts 版本
配置 tsconfig
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