如何创建嵌套的 d.ts 文件?

How can create a nested d.ts file?

我正在构建一个可以在 JS 和 TS 中使用的模块。 据我所知,在JS中创建模块时,创建一个单独的d.ts文件。(当然可以使用TS编译成JS)。不管怎样,我选择制作 d.ts 文件。

例如

// common.d.ts

declare namespace common {
  export const method: string => string;
}
export = common;

接下来,

// utils.d.ts
import * as u from './common';
declare namespace utils {
  export const common: u // Causing an error.
}
export = utils;

我收到了这个错误:The namespace 'u' can not be used as a format. 想写这个统一导入地址。

import { common as u } from '/utils';
u.method('Any params');

我想,也许我可以从 t.ds 文件中获取声明并分配它们。但是我不知道怎么做。谁帮帮我!

export const common: u替换为export const common = u将消除错误,但只会复制u的值含义; common.d.ts 中定义的接口和类型别名将无法通过此 common 常量访问。 export import common = u 更好:它复制所有含义。但是大多数人会认为,删除名称空间和导出分配的无意义组合并将单个导出从名称空间移至模块的顶层会更好;那么你可以使用 ECMAScript export as 语法而不是 TypeScript 别名。

// common.d.ts
export const method: string => string;

// utils.d.ts
import * as u from './common';
export { u as common };