Typescript 在导入 Node.js 时找不到自己的名称 class

Typescript cannot find name of own class when importing Node.js

我正在尝试在我定义的 class 中使用 node.js class(在本例中为净值)。

Connection.ts:

import net = require('net');
class Connection  {

}

然后我在另一个文件中引用我的 class,

ConnectionContext.ts:

interface IConnectionContext {
    connection: Connection;
}

当我有这段代码时,我在 ConnectionContext 中看到一个带下划线的 "Connection",上面写着 "IConnectionContext.ts(3,17): error TS2304: Cannot find name 'Connection'"

如果我从部分中删除 import * as net,编译错误就会消失。

我有点困惑,当我查看生成的 JavaScript 时,我所有的 classes 在创建时看起来都一样。我不确定为什么打字稿说 class 不存在。

这是我的 tsconfig:

{
    "compileOnSave": true,
    "compilerOptions": {
        "module": "commonjs",
        "outDir": "bin/",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": true,
        "moduleResolution": "node",
        "target": "es5"
    },
    "exclude": [
        "node_modules"
    ]
}

基于此,我需要做什么才能让我的界面看到我的 class 使用 node.js 模块?

if I remove the import * as net from part, the compile error goes away.

在缺少 importexport 语句的情况下,该文件被视为 全局 。一旦您导入/导出它就成为一个模块,因此必须导入。

此处介绍:https://basarat.gitbooks.io/typescript/content/docs/project/modules.html