2017-10-12 88 views
0

我是新來的打字稿,我想要做的是將我的打字稿文件轉換爲javascript 當我做npm打字稿--verison時,我得到3.10.10無法編譯.ts到.js在Vs - windows

我有我的task.json文件中像這樣

{ 
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format 
    "version": "0.1.0", 
    "command": "${cwd}/node_modules/.bin/tsc.cmd", 
    "isShellCommand": true, 
    "args": [ 
     "-p", 
     "." 
    ], 
    "showOutput": "silent", 
    "problemMatcher": "$tsc" 
} 

tsconfig.json文件中像這樣

{ 
    "compileOnSave": false, 
    "compilerOptions": { 
    "outDir": "./dist/out-tsc", 
    "sourceMap": true, 
    "declaration": false, 
    "moduleResolution": "node", 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "target": "es5", 
    "typeRoots": [ 
     "node_modules/@types" 
    ], 
    "lib": [ 
     "es2016", 
     "dom" 
    ] 
    } 
} 

當我這樣做TSC mock.ts TSC,我得到這樣

「TSC」錯誤沒有被識別爲一個內部或外部的命令,可操作 程序或批處理文件。

從我在tasks.json文件

「命令」 用這個博客的一個小小的研究後: 「$ {} CWD /node_modules/.bin/tsc.cmd」, 但我不能夠編譯我的.ts文件轉換爲JavaScript

回答

0

有兩點要注意,希望能幫助

  • npm typescript --verison正在返回版本的npm,而不是TypeScript(3.1已過時,因此您可能需要更新第一個npm i -g npm
  • 運行tsc的方式取決於您是全局安裝(npm install typescript)還是本地安裝(npm install --save-dev typescript) 。

如果你添加一個腳本到你的package.json文件,它應該爲你處理查找typescript包,無論它是全局安裝還是本地安裝。例如:

中的package.json

... "scripts": { "tsc": "tsc" } ...

然後運行打字稿編譯器,像這樣:npm run tsc -- <your typescript options>。例如:npm run tsc -- --version應該返回打字稿的實際版本(或者告訴你TypeScript沒有真正安裝)。

如果您想進一步深入研究是否安裝了TypeScript,您還可以結賬npm ls