Using the custom attribute on the global window, typescript will report that the attribute does not exist,
console.log(window.foo) // ❌ Property ‘foo’ does not exist on type 'Window & typeof globalThis'.ts(2339)
The user-defined variable needs to be extended to the global window. You can add a type file or a normal. TS file in the project, as long as it can be found within the configuration scope of tsconfig.json.
type.d.ts
declare global {
interface Window {
foo: string;
}
}
If the following error occurs during type extension:
Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.ts(2669)
The following contents can be added to the type file to take the specified file as the template and eliminate the error.
+ export {};
declare global {
interface Window {
foo: string;
}
}
Similar Posts:
- [Solved] Windows Variable packing error: Property ‘XXX‘ does not exist on type ‘Window‘
- error: command ‘gcc’ failed with exit status 1
- [Solved] Vue Error: Error in render: “TypeError: Cannot read property ‘length‘ of undefined“
- Error: jest not implemented window.open() [How to Solve]
- Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers
- The Difference between $(document).ready, window.onload and $(window).load(function (){})
- How to Solve Windows setup encountered HResult: 0x80070422 Error
- shadows name ‘xxxx’ from outer scope
- [Solved] Python SyntaxWarning: name ‘xxx’ is assigned to before global declaration
- [Solved] Lexical declaration cannot appear in a single-statement context