【问题标题】:What would be the utility of the declare keyword in this example在这个例子中,declare 关键字的用途是什么
【发布时间】:2021-12-26 23:28:24
【问题描述】:

我有以下d.ts 文件(请注意命名空间D3 之前没有declare 关键字,d3 之前也没有):

namespace D3 {
  export interface Selectors {
    select: {
      (selector: string): Selection;
      (element: EventTarget): Selection;
    };
  }
  export interface Event {
    x: number;
    y: number;
  }
  export interface Base extends Selectors {
    event: Event;
  }
}

const d3: D3.Base;

在其他文件中,我可以访问d3,我知道这是因为 d.ts 文件没有导入/导出,所以它被视为“全局脚本”。

我还了解到,declare 关键字用于环境声明,您希望使用可能不是源自 TypeScript 文件的变量。

我不明白的是,如果没有它一切正常,为什么我需要在 d3 之前添加 declare 关键字。

谢谢!

【问题讨论】:

    标签: typescript


    【解决方案1】:

    刚刚发现我可以省略 declare 关键字,因为我在 tsconfig.json 中启用了 skipLibCheck,它(顾名思义)跳过了声明文件的类型检查:

    https://www.typescriptlang.org/tsconfig#skipLibCheck

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 1970-01-01
      • 2014-05-26
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      • 2011-10-26
      • 2013-09-20
      相关资源
      最近更新 更多