首页 > 解决方案 > Typescript:在类型定义文件中导入其他类型

问题描述

我的一个模块有一个类型定义文件,它有很多接口的定义。我正在尝试将定义分解为多个文件以便更好地组织

// original file - index.d.ts
declare module 'my-module' {
  interface A { }
  interface B { }
  interface C { }
  export interface D {
    a: A,
    b: B,
    c: C
  }
}

// 我正在寻找的设置

// A.d.ts
export interface A {}

// B.d.ts 
export interface B {}

// index.d.ts
declare module 'my-module' {
  // import A.d.ts here
  // import B.d.ts here
}

我如何导入A.d.tsB.d.ts输入index.d.ts

我看到它工作的一种方法是使用import()语法,我可以在其中执行以下操作:

declare module 'my-module' {
   export interface D {
     a: import('../A.d.ts').A
     ...
   }
}

我也知道文件顶部的常规导入不起作用,因为它将全局声明更改为模块,但是为什么在声明关键字中导入不起作用?

declare module 'my-module' {
  import { A } from '../A.d.ts';
  export interface D {
    a: A
    ...
  }
}

标签: javascripttypescript

解决方案


推荐阅读