Visual Studio代码:编译typescript模块 [英] Visual Studio Code: compile typescript module

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

问题描述

我刚刚下载了新的Visual Studio代码,我的第一印象是非常积极的。



但是,有一个奇怪的问题:VS Code似乎不能编译typescript模块。



此代码:

  ///< reference path =../ definitions / /.data/> 

import React = require(react);

在cmd上编译完全正确,



< blockquote>

tsc --module commonjs main.ts


但在VS代码中,第二行突出显示红色,并且编辑器抱怨:


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


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



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



UPDATE



我添加了tsconfig.json文件:

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

这实际上会移除错误。不幸的是,IDE不再编译我的代码。起初我认为config.json只会使错误消息静默,但它的确比这更多。 Intellisense现在可以在示例文件中工作。如果我键入 React ,自动完成被触发,显然知道React,因为显示有意义的建议。



't VS代码编译文件到js?我尝试配置任务运行器来完成这项工作,但它似乎不起作用:

  {
version:0.1.0,

//命令是tsc。
command:tsc,

//仅当发生无法识别的错误时才显示输出窗口。
showOutput:silent,

//在windows下使用tsc.exe。这确保我们不需要一个shell。
windows:{
command:tsc.exe
},

// args是要编译的HelloWorld程序。
args:[--module commonjs,$ {file}],

//使用标准的tsc问题匹配器找到编译问题
//在输出。
problemMatcher:$ tsc
}

文件,没有任何反应,即使我显式运行构建任务,没有响应。我编辑的任务的名称是tsc,我试图运行它,太。没有效果。然后我将参数更改为args:[--module commonjs,main.ts] ,无响应。



UPDATE



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



< blockquote>

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


以下是输出结果:




  • args:[-p],

  • [-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:true,


$ b b

所以,visual studio代码试图在命令行上运行命令 tsc ,它没有找到 tsc 。所以,使用npm在全球安装打字稿解决了这个问题。


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代码:编译typescript模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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