node.js - 带有 TypeScript 的 Nodejs 会出现奇怪的错误

标签 node.js npm typescript visual-studio-code

我正在使用 TypeScript 1.5.0-beta,我正在使用 Visual Studio Code 使用 Anders Hejlsberg 在构建时展示的示例创建一个简单的 Node 服务器。

///<reference path="typings/node/node.d.ts"/>

import { createServer } from "http"

export function simpleServer(port: number, message: string)
{
  createServer((req, res) =>
  {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
  }).listen(port, '127.0.0.1');

  console.log('Server running at http://127.0.0.1:1337/');
}

我有三个问题

1) 使用此代码,每当我尝试使用 Visual Studio Code 或命令 $ tsc server -m commonjs 构建 ts 文件时,它都会不断出现类似这些的奇怪错误

error TS1008: Unexpected token; 'module
, class, interface, enum, import or statement' expected

2) 它还尝试构建我正在引用的 node.d.ts 文件并提供额外的“:”预期错误消息。

3) 这更像是一个 Visual Studio Code 问题。我们如何让 VS Code 构建项目/文件夹中的所有 ts 文件并生成 js 文件。 我按照 VS 代码的 typescript 设置说明进行操作,但我仍然必须单独构建每个 ts 文件以生成 js 代码。

我知道 grunt 是一种方法,但据我所知,VS Code 应该能够像 Visual Studio 那样在一个文件夹中构建所有 ts 文件。如果我错了,请纠正我。

谢谢。

最佳答案

好吧,我终于弄清楚发生了什么事。由于我安装了 Visual Studio 2013 和 TypeScript 1.4,它在 C:/Program Files (x86)/Microsoft SDKs/TypeScript/1.0 下创建了文件。因此,每次我使用 tsc 命令时,它总是默认为 1.0.3.0 版本,它适用于 TypeScript 1.4 而不是 1.5.0-beta。当我通过 Node 安装 TypeScript 1.5.0-beta 时,它在 C:\Users\Krishna V\AppData\Roaming\npm\tsc 下创建了新版本的文件

因此,对于 #1,为了解决编译版本问题,我更改了 task.json 文件以使用命令和 Windows 选项的完整路径。所以,它看起来像这样

{
    "version": "0.1.0",

    // The command is tsc.
    "command": "C:\\Users\\Krishna^ V\\AppData\\Roaming\\npm\\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": "C:\\Users\\Krishna^ V\\AppData\\Roaming\\npm\\tsc"
    },

    // args is the HelloWorld program to compile.
    "args": [],

    "isShellCommand": true,

    // use the standard tsc problem matcher to find compile problems
    // in the output.
    "problemMatcher": "$tsc"
}

对于 #2,Node.d.ts 与 TypeScript 1.5.0-beta 不同步似乎是一个已知问题,为此在 git 中打开的 react.d.ts 已经存在类似问题。

对于 #3,tsconfig.json 仅适用于 TypeScript 版本 1.5.0-beta,并且由于 tsc 使用了错误的版本,它从未使用 tsconfig。现在它使用了正确的版本,它使用了 tsconfig,因此它构建了 tsconfig.json 中提到的所有文件。例如,其中任何一个(ES5 和 ES6)

{
    "compilerOptions": {
        "target": "ES5",
        "module": "commonjs",
        "sourceMap": true,
        "removeComments": true
    },
    "files": [
        "server.ts","config.ts","models/user.ts"
    ]
}

OR 

{
    "compilerOptions": {
        "target": "ES6",
        "sourceMap": true,
        "removeComments": true
    },
    "filesGlob": [
        "./**/*.ts"
    ]
}

在这种情况下,它将编译第一个选项中提到的三个文件或选项2中的所有ts文件。

关于node.js - 带有 TypeScript 的 Nodejs 会出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30065069/

相关文章:

javascript - Gulp 不会覆盖 JS 文件

node.js - Controller 无法将响应传递给 index.js

javascript - 在 TypeScript 中,如何在外部模块中扩展环境 JQuery 接口(interface)?

javascript - 是否可以指定来自 props 的数组长度?

angular - rxjs/运营商 : can not find module

javascript - 从 url 调用 Nodejs 函数

mysql - 如何在插入数据之前检查nodejs中的mysql表字段类型

npm - 通过 NPM 安装 bulma 后,如何在我的项目中引用它

angular - ng build --prod 有 javaScript 堆内存不足错误

javascript - 关于Rxjs的基本问题