w7a9q
w7a9q2mo ago

Types for global variables

I have defined a global variable: globalThis.Args = validateArguments(Deno.args); Now whenever I use it I get TS error: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.deno-ts(7017) How can I fix this?
1 Reply
m4rc3l05
m4rc3l052mo ago
Hi. Try to create a new d.ts file like global.d.ts with the contents
// deno-lint-ignore no-var
declare var Args: Record<string, unknown>;
// deno-lint-ignore no-var
declare var Args: Record<string, unknown>;
Add a reference to this d.ts file in the compiler options in deno.json
{
//...
"compilerOptions": {
"types": ["./global.d.ts"]
},
//...
}
{
//...
"compilerOptions": {
"types": ["./global.d.ts"]
},
//...
}
You should be able to access Args from globalThis this should also pass the deno check command to typecheck your code

Did you find this page helpful?