我刚刚下载了新的Visual Studio Code,我的第一印象非常积极.对于打字稿,intellisense工作得很漂亮.
但是,有一个奇怪的问题:VS Code似乎无法编译typescript模块.
这段代码:
/// <reference path="../definitions/react.d.ts"/>
import React = require("react");
Run Code Online (Sandbox Code Playgroud)
在cmd上编译完全正常
tsc --module commonjs main.ts
但在VS Code中,第二行以红色突出显示,编辑抱怨:
除非提供"-module"标志,否则无法编译外部模块
当然,任何使用模块的打字稿代码都必须使用此标志进行编译.但是如果IDE知道模块的用法,为什么不设置标志呢?没有模块的打字稿代码在保存时编译,没有问题.
我想我错过了一些编译器设置配置文件.有这样的事吗?我在哪里可以找到它?
我添加了tsconfig.json文件:
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"sourceMap": true
}
}
Run Code Online (Sandbox Code Playgroud)
这实际上消除了错误.不幸的是,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"
}, …Run Code Online (Sandbox Code Playgroud)