首页 > 解决方案 > 从 typescript 定义 d.ts 文件动态加载接口

问题描述

我有一个.d.ts包含接口的文件。我不知道预先存在哪些接口。这些.d.ts文件是由第三方库生成的,而不是从相应的文件中生成的.ts

export interface ExampleInterface1 {
  id: string;
  type: string;
}

export interface ExampleInterface2 {
  id: string;
  type: string;
}

因此,我想动态加载`.d.ts

const data = await import('path/to/my/dynamically/created/file'); // without .d.ts extension

但是,这不适用于.d.ts文件。我用其他文件进行了测试,包括 JSON,并且内容被很好地加载到data变量中。

我可以手动导入,但当然,这对我的情况没有帮助。

import { ExampleInterface1, ExampleInterface2 } from 'path/to/my/dynamically/created/file';

如何从.d.ts文件中动态加载接口名称和内容?

标签: javascripttypescriptdynamicimport

解决方案


推荐阅读