Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在typescript中扩展全局属性?_Typescript - Fatal编程技术网

如何在typescript中扩展全局属性?

如何在typescript中扩展全局属性?,typescript,Typescript,src/window.d.ts declare namespace NodeJS { interface Global { window: { location: {}; }; } } 在我的源代码中,我试图访问global.window.location。我收到一个错误类型Global上不存在属性“window” 我不知道typescript编译器是否正在使用我的window.d.ts?如何告诉编译器使用我的自定义类型

src/window.d.ts

declare namespace NodeJS  {
    interface Global {
        window: {
            location: {};
        };
    }
}
在我的源代码中,我试图访问
global.window.location
。我收到一个错误
类型Global上不存在属性“window”


我不知道typescript编译器是否正在使用我的
window.d.ts
?如何告诉编译器使用我的自定义类型?

将此文件路径添加到
文件的
数组
tsconfig.json
,这样TS将熟悉它,并扩展真正的窗口对象

顺便说一句,为了向全局窗口对象添加属性:

declare global {
  interface Window {
    location: {};
  }
}