【问题标题】:Augmenting global in TypeScript在 TypeScript 中增强全局
【发布时间】:2019-10-06 23:26:00
【问题描述】:

如何在 TypeScript 中将fetch 添加到全局对象中,这样我就不必这样做了:

(global as any).fetch

我已经在 ./types/index.d.ts 的文件中尝试过这个

并尝试通过将其包含在 tsconfig.json 中来引用它。

【问题讨论】:

  • 我们说的是节点global对吧?
  • 是的,先生,我确实是

标签: typescript declaration-files


【解决方案1】:

如果您在global 上使用Go to Definition,您将看到declared in node definition files 为:

declare var global: NodeJS.Global; 

因此,为了向其中添加内容,您只需在 NodeJS 命名空间中扩充 Global 接口:

declare namespace NodeJS {
    export interface Global {
        fetch(u: string): string
    }
}
global.fetch("") // ok now

【讨论】:

  • 说我在 index.d.ts 文件中有这个。我需要将此文件添加到 tsconfig.json 中的路径吗?
  • @dagda1 如果没有以任何其他方式将其添加到编译路径,那么是的,您需要将其添加到 tsconfig。
  • 没关系,我在这里找到了答案typescriptlang.org/docs/handbook/…
猜你喜欢
  • 1970-01-01
  • 2016-08-22
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
  • 2017-04-01
  • 2018-04-05
  • 2018-03-26
相关资源
最近更新 更多