如何在VScode中设置打字稿?

时间:2017-02-18 07:46:28

标签: javascript typescript compiler-errors visual-studio-code

我单独使用VS Code typescript配置。

https://code.visualstudio.com/Docs/languages/typescript

我已将tsconfig.json设置为

{
    "compilerOptions": {
        "module": "system",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "outFile": "built/local/tsc.js",
        "sourceMap": true
    },
    "include": [
        "**/*"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

和我的任务选手

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

我的代码

class StartUp {
    public static main(): number {
        console.log('Helle Workd');
        return 5;
    }
}
console.log('test');

StartUp.main();

出于某种原因,当我按cmd + shift + B(build)时,我在输出窗口中看不到任何输出。我确实看到像

这样的错误
hello.ts(8,1): error TS2304: Cannot find name 'ddd'.

如果我在代码中添加随机添加ddd字符串。

有人可以帮我解决这个问题吗?非常感谢!

2 个答案:

答案 0 :(得分:1)

命令" tsc -p。"如果编译时没有错误并且生成了所有已编译的JavaScript / SourceMap文件,则不会输出任何内容,因此您无法在VSCode的输出窗口中看到任何错误。只需在控制台中输入并运行命令即可。

您可以添加选项" - 诊断"使命令输出一些信息。

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

输出:

Files:            2
Lines:        18225
Nodes:        73338
Identifiers:  24828
Symbols:      18969
Types:         4609
Memory used: 62579K
I/O read:     0.00s
I/O write:    0.01s
Parse time:   0.24s
Bind time:    0.12s
Check time:   0.54s
Emit time:    0.06s
Total time:   0.96s

另请参阅http://www.typescriptlang.org/docs/handbook/compiler-options.html

中的所有选项

答案 1 :(得分:0)

设置应用结构 设置基本的HTML     npm init - 是的     npm install lite-server --save-dev     npm install --save-dev typescript @ types / node @ types / jasmine @ types / core-js     将脚本lite添加到package.json中的scrips中     创建一个app文件夹     添加app.js.    创建tsconfig.json    添加以下选项

{
 "compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"lib": ["es2015", "dom"]
 }
}

将以下内容添加到package.json文件

{


"name": "Angular2",

"version": "1.0.0",

"description": "",

"main": "index.js",

"scripts": 
{

    "start": "concurrently \"npm run tsc:w\" \"npm run lite\"",

    "lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w"
},

    "keywords": [],
"author": "",
 "license": "ISC",
 "devDependencies": 
{

    "@types/core-js": "^0.9.43",

    "@types/jasmine": "^2.8.2",

    "@types/node": "^8.0.53",

    "concurrently": "^3.5.1",

    "lite-server": "^2.3.0",

    "typescript": "^2.6.1"
  },


  "dependencies": 
{

    "types": "^0.1.1"

}

}