typescript - 命名空间问题未定义

typescript - namespace problem not defined

这是我的客户class:

namespace Core {
    export class Client {}
}

然后我创建了一个如下所示的新对象:

let client = new Core.Client();

但是我得到这个错误:

/dist/index.js:10
    let client = new Core.Client()
                 ^

ReferenceError: Core is not defined
    at Namespace.<anonymous> (/dist/index.js:10:18)
    at Namespace.emit (events.js:314:20)
    at Namespace.emit (/node_modules/socket.io/lib/namespace.js:213:10)
    at /node_modules/socket.io/lib/namespace.js:181:14
    at processTicksAndRejections (internal/process/task_queues.js:79:11)

有什么问题?

您需要在 index.ts 中引用您的命名空间。因此,如果 Client 在名为 core.ts 的文件中,您需要执行以下操作:

/// <reference path="core.ts" />
let client = new Core.Client();

并且您需要导出您的命名空间:

export namespace Core {
    export class Client {}
}

有关详细信息,请参阅 handbook