我刚刚下载了新版的Visual Studio Code,我的第一印象非常好。对于TypeScript,智能感知效果非常出色。
但是,有一个奇怪的问题:VS Code似乎无法编译TypeScript模块。
这段代码:
/// <reference path="../definitions/react.d.ts"/>
import React = require("react");
在命令提示符(cmd)中,使用
tsc --module commonjs main.ts
可以编译成功。但是在VS Code中,第二行会被标记成红色并报错:
cannot compile external modules unless the "-module" flag is provided
当然,任何使用模块的TypeScript代码都必须使用该标志进行编译。但是,如果IDE意识到使用模块,为什么它不设置该标志呢?没有模块的TypeScript代码可以在保存时编译,没有问题。
我认为我缺少一些编译器设置配置文件。这种文件存在吗?在哪里可以找到它?
更新
我已经添加了tsconfig.json文件:
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"sourceMap": true
}
}
这实际上消除了错误。不幸的是,IDE不再编译我的代码。起初我以为config.json只会消除错误消息,但它做得更多。现在示例文件中的Intellisense可以工作了。如果我键入React
,自动完成就会触发,并且显然知道React,因为显示了有意义的建议。
那么,为什么VS Code不将文件编译为js?我尝试配置任务运行程序来完成这项工作,但似乎不起作用:
{
"version": "0.1.0",
// The command is tsc.
"command": "tsc",
// Show the output window only if unrecognized errors occur.
"showOutput": "silent",
// Under windows use tsc.exe. This ensures we don't need a shell.
"windows": {
"command": "tsc.exe"
},
// args is the HelloWorld program to compile.
"args": ["--module commonjs","${file}"],
// use the standard tsc problem matcher to find compile problems
// in the output.
"problemMatcher": "$tsc"
}
如果我保存文件,即使我明确运行构建任务,也没有任何反应。我编辑的任务名称是“tsc”,我也尝试运行它。没有效果。然后我将参数更改为"args": ["--module commonjs","main.ts"]
,仍然没有反应。
更新
似乎任务运行器唯一可行的设置是这两个:
"args": ["${file}"],
"isShellCommand": true,
以下是输出结果:
"args": ["-p"],
"args": ["-p", "."],
错误 TS5023:未知的编译器选项 'p'。
"args": ["."],
错误 TS6053:找不到文件 '.ts'。
import * as React from 'react'
пјҢ并еңЁtsconfig.json
ж–Ү件дёӯжҢҮе®ҡжӮЁзҡ„жЁЎеқ—зұ»еһӢгҖӮ - einsteincmd
+shift
+p
并配置你的任务运行器。它应该在args
属性下面。 - einstein