vscode下配置Typescript編譯環境
步驟
- 首先需要安裝Nodejs
node -v
- 安裝typescript模塊
npm install -g typescript
- 可查看typescript模塊版本號
tsc -v
- 在
window + r
—> 輸入cmd
進入該項目目錄, 或 項目中Ctrl + ~
, 創建tsconfig.json tsc --init
- 在項目目錄下會生成一個tsconfig.json文件
- 默認: 大部分是註釋的, 可以根據各自的需要來定製, tsconfig.json詳解
tsconfig.json配置
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"allowJs": true
"sourceMap": true,
"outDir": "./js/",
"rootDir": "./tscript/",
"removeComments": false,
"noImplicitAny": false,
...更多配置項
},
vscode 自動編譯Typescript
- 快速打開終端
Ctrl + ~
- 輸入: cmd .
- tsc -w
測試
- 例: 新建tscript,用作存放typescript文件**【tsconfig.json中配置輸出文件夾】**。
- tscript —>建 .ts後綴測試文件
var test:number = 4;
- Ctrl + s 保存, 自動編譯 Typescript文件
- 點擊拆分編輯器, 右邊可時時預覽編譯後的**.js文件**