簡體   English   中英

在 TypeScript 中增強全局

[英]Augmenting global in TypeScript

如何將fetch添加到 TypeScript 中的全局對象上,這樣我就不必這樣做了:

(global as any).fetch

我已經在./types/index.d.ts的文件中嘗試過這個

並嘗試通過將其包含在 tsconfig.json 中來引用它。

如果您在global上使用Go to Definition ,您將看到它在節點定義文件中聲明為:

declare var global: NodeJS.Global; 

所以為了給它添加東西,你只需要在NodeJS命名空間中增加Global接口:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM