Update 2021 October
Applies to TypeScript 4.3+
Error
Element implicitly has an
any
type because typetypeof globalThis
has no index signature. ts(7017)
Solution
declare global {
function myFunction(): boolean;
var myVariable: number;
}
globalThis.myFunction = () => true;
globalThis.myVariable = 42;
- IMPORTANT: This solution only works by declaring variables with
var
(do not uselet
orconst
).
Background
See the discussion on TypeScript issue 30139.
Traditionally, the way to specify a TypeScript declare-block in a Node.js context was as follows:
// Does not work as of October 2021 (TypeScript 4.3+)
declare global {
module NodeJS {
interface Global {
myFunction(): boolean;
myVariable: number;
}
}
}
tsconfig.json: noImplicitAny
Note that this error will be suppressed if the TypeScript setting noImplicitAny
is set to false
. It is recommended to enable noImplicitAny
for better type checking.