【问题标题】:How can create a nested d.ts file?如何创建嵌套的 d.ts 文件?
【发布时间】:2019-03-30 11:15:23
【问题描述】:

我正在构建一个可以在 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 文件中获取声明并分配它们。但是我不知道怎么做。谁能帮帮我!

【问题讨论】:

    标签: typescript import namespaces export declare


    【解决方案1】:

    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 };
    

    【讨论】:

      猜你喜欢
      • 2020-04-19
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      • 2017-03-02
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      相关资源
      最近更新 更多