【问题标题】:In a definition file, is it possible to extend from an imported module directly?在定义文件中,是否可以直接从导入的模块扩展?
【发布时间】:2021-09-22 19:27:22
【问题描述】:

在 d.ts 文件中,我想执行以下操作:

interface A extends import("some-module").B
{
   //...
}

我能够做到这一点的唯一方法是导入我从第一个扩展的类型:

type ExternalB = import("some-module").B

interface A extends ExternalB
{
    //...
}

【问题讨论】:

  • 你的第二个例子有什么问题?您为什么要寻找替代品?
  • 没有任何问题,而且运行良好。我只是在寻找一种不涉及创建新类型的更清洁的解决方案。

标签: typescript


【解决方案1】:

这是不允许的,和在 Playground 中一样,它会给出错误 #2499。

接口只能使用可选类型参数扩展标识符/限定名。 (2499)

这也和similar questions on SO一致:你得先定义类型。作为一种预感,这可能是为了与 ECMAScript extends definition 兼容。

但是,您可以动态地进行类型交集。你甚至可以extend the resulting type as an interface,假设结果是一个对象类型。

type A2 = import("some-module").B & {
    a2(): void;
}
interface A3 extends A2 {
    a3(): void;
}

Playground Link

【讨论】:

  • 我很怀疑 - 感谢您提供更多信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多