简体   繁体   English

TypeScript 扩充 Window 不在单独的文件中工作

[英]TypeScript Augmenting Window not working in separate files

I'm looking to add another property to window .我希望向window添加另一个属性。 I can do that with this:我可以这样做:

// global.d.ts

import { IConfig } from './src/models';
export {};

declare global {
    interface Window {
        _env: IConfig;
    }
}

But then when I try to reference this new property in a different file, it complains:但是当我尝试在不同的文件中引用这个新属性时,它会抱怨:

// src/util.ts

// Property '_env' does not exist on type 'Window & typeof globalThis'.
export const URL = `https://example.com/${window._env.path}`;

But when I combine these into the same file, everything is fine and there are no errors.但是当我将它们组合到同一个文件中时,一切都很好,没有错误。 Is there anyway I can have these in a separate file?无论如何我可以将这些放在单独的文件中吗?

I'm using TypeScript 4.1.2.我正在使用 TypeScript 4.1.2。

I went down a bit of a rabbit hole but found this relevant documentation我掉进了一个兔子洞,但找到了这个相关文档

I was able to accomplish this exactly as you have written (different type of course) in an existing angular 8 project and svelte project using their existing polyfills.ts files for my global declaration.我能够完全按照您在现有 angular 8 项目和 svelte 项目中编写的(当然是不同类型)使用他们现有的 polyfills.ts 文件来完成我的全局声明。

Are you sure you tsconfig.json is compiling everything correctly?你确定你 tsconfig.json 正在正确编译一切吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM