Applies to TypeScript 4.3+
xxxxxxxxxx
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 use let or const).
xxxxxxxxxx
/* Create global.d.ts */
// extend the global object with "declare global"
declare global {
type Example = {
foo: string
bar: number
}
}
// type "Example" will now be available anywhere without import
xxxxxxxxxx
// @ts-ignore
console.log("globalThis.myVar: "+globalThis.myVar);
// This will ignore the error typescript gives when using globalThis