Visual Studio Code:编译打字稿模块 [英] Visual Studio Code: compile typescript module

查看:27
本文介绍了Visual Studio Code:编译打字稿模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚下载了新的 Visual Studio Code,我的第一印象非常好.对于打字稿,智能感知效果很好.

然而,有一个奇怪的问题:VS Code 似乎无法编译 typescript 模块.

此代码:

///<参考路径="../definitions/react.d.ts"/>import React = require("react");

在 cmd 上编译得很好,使用

<块引用>

tsc --module commonjs main.ts

但在 VS Code 中,第二行以红色突出显示,编辑器抱怨:

<块引用>

除非提供-module"标志,否则无法编译外部模块

当然,任何使用模块的打字稿代码都必须使用此标志进行编译.但是如果 IDE 知道模块的使用,为什么不设置标志?没有模块的打字稿代码在保存时编译,没有问题.

我想我缺少一些编译器设置配置文件.有这种事吗?我在哪里可以找到它?

更新

我添加了 tsconfig.json 文件:

<代码>{编译器选项":{"目标": "ES5","module": "commonjs","sourceMap": 真}}

这实际上消除了错误.不幸的是,IDE 不再编译我的代码.起初我以为 config.json 只会使错误消息静音,但它的作用不止于此.Intellisense 现在可以在示例文件中使用.如果我输入 React 自动完成被触发并且显然知道 React 因为有意义的建议被显示.

现在,为什么 VS Code 不将文件编译为 js ?我试图配置任务运行器来完成这项工作,但它似乎不起作用:

<代码>{版本":0.1.0",//命令是 tsc.命令":tsc",//仅当发生无法识别的错误时才显示输出窗口."showOutput": "沉默",//在 windows 下使用 tsc.exe.这确保我们不需要外壳.窗户":{命令":tsc.exe"},//args 是要编译的 HelloWorld 程序."args": ["--module commonjs","${file}"],//使用标准的 tsc 问题匹配器来查找编译问题//在输出中."problemMatcher": "$tsc"}

如果我保存文件,没有任何反应,即使我明确运行构建任务,也没有响应.我编辑的任务名称是tsc",我也尝试运行它.没有效果.然后我将参数更改为 "args": ["--module commonjs","main.ts"],无响应.

更新

任务运行器的唯一工作方式是使用这两个设置:

<块引用>

"args": ["${file}"],isShellCommand":真,

输出如下:

  • "args": ["-p"],
  • "args": ["-p", "."],
<块引用>

错误 TS5023:未知的编译器选项p".

  • "args": ["."],
<块引用>

错误 TS6053:找不到文件.ts".

解决方案

我今天也遇到了同样的问题.我跟着这个链接http://blogs.msdn.com/b/typescript/archive/2015/04/30/using-typescript-in-visual-studio-code.aspx完成所有设置步骤后,我在命令行上运行此命令并开始生成 JavaScript 文件

npm install -g typescript

我们需要确保我们已经安装了 node 和 npm,并且可以通过命令行访问.我发现它不起作用的原因是在 tasks.json 中我们指定了以下选项

"command": "tsc"isShellCommand":真,

因此,visual studio code 尝试在命令行上运行命令 tsc 并且找不到 tsc.所以,使用 npm 全局安装 typescript 解决了这个问题.

I've just downloaded the new Visual Studio Code and my first impression is very positive. For typescript, intellisense works beautifully.

However, there is a strange problem: VS Code doesn't seem to be able to compile typescript modules.

This code:

/// <reference path="../definitions/react.d.ts"/>

import React = require("react");

compiles perfectly fine on the cmd, with

tsc --module commonjs main.ts

but within VS Code, the second line is highlighted in red and the editor complains:

cannot compile external modules unless the "-module" flag is provided

Of course any typescript code which makes use of modules has to be compiled with this flag. But if the IDE is aware of the usage of modules, why doesn't it set the flag ? Typescript code without modules is compiled on save, without problems.

I think I'm missing some compiler-setup config file. Is there such a thing ? Where can I find it ?

UPDATE

I've added the tsconfig.json file:

{
    "compilerOptions": {
        "target": "ES5",
        "module": "commonjs",
        "sourceMap": true
    }
}

This actually removes the error. Unfortunately the IDE no longer compiles my code. At first I thought the config.json would only silence the error message, but it does more than that. Intellisense now works in the sample file. If I type React the autocompletion is triggered and apparently knows React because meaningful suggestions are displayed.

Now, why doesn't VS Code compile the file to js ? I've tried to configure the task runner to do that job, but it doesn't seem to work:

{
    "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"
}

If I save the file, nothing happens, even if I explicitly run the build task, there's no response. The name of the task I edited is "tsc", I tried to run that, too. No effect. Then I changed the arguments to "args": ["--module commonjs","main.ts"], No response.

UPDATE

The only way the task runner seems to work is with these two settings:

"args": ["${file}"], 
"isShellCommand": true, 

Here are the outputs:

  • "args": ["-p"],
  • "args": ["-p", "."],

error TS5023: Unknown compiler option 'p'.

  • "args": ["."],

error TS6053: File '.ts' not found.

解决方案

I also faced the same problem today. I followed this link http://blogs.msdn.com/b/typescript/archive/2015/04/30/using-typescript-in-visual-studio-code.aspx After following all setup steps, I ran this command on command line and it started generating JavaScript files

npm install -g typescript

We need to ensure that we have node and npm installed and accessible through command line. The reason I found it was not working because in tasks.json we specify following options

"command": "tsc"
"isShellCommand": true,

So, visual studio code tries to run command tsc on command line and it does not find tsc. So, installing typescript globally using npm solved the problem.

这篇关于Visual Studio Code:编译打字稿模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆