gpt4 book ai didi

javascript - 使用 AMD 在 Typescript 中引用文件(模块)

转载 作者:行者123 更新时间:2023-11-28 00:03:30 25 4
gpt4 key购买 nike

我是 Typescript 的新手和模块处理。在我的项目中,我使用 Typescript 进行编码以开发浏览器库。因此我使用 AMD 。以下是 tsconfig.json 文件。

{
"compilerOptions": {
"target": "ES5",
"outDir": "out",
"module": "amd"
},
"files": [
"src/main.ts",
"src/communication.ts",
...
]
}

文件communication.ts是:

export module MyProj.DataExchange {
export interface Communication {
connect(uri: string): void;
close(): void;
status: int;
}
}

从其他文件引用组件

我想在 main.ts 中使用Communication:

import communication = require('./communication');
export module MyProj {
export class Communicator
implements communication.MyProj.DataExchange.Communication {
...
}
}

我想避免使用整个签名communication.MyProj.DataExchange.Communication。所以我尝试了类似的方法:

import communication = require('./communication').MyProj.DataExchange;

但是没有成功。

我的问题

我有一种感觉,我在这里做错了什么。这是我的问题:

  • 我是否按照应有的方式做事?
  • 如何避免使用导入组件的全名?
  • 如果您可能告诉我不要使用模块,我需要将我的组件分隔到命名空间中。那么如果我做错了,如何正确设置 namespace 呢?

最佳答案

在 Typescript 1.4 中已引入 Type Aliases .

您的代码可能适合使用这样的别名:

import communication = require('./communication')
type Communication = communication.MyProj.DataExchange.Communication;
export module MyProj {
export class Communicator
implements Communication {
...
}
}

关于javascript - 使用 AMD 在 Typescript 中引用文件(模块),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31568386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com