globalThis is the future.
First, TypeScript files have two kinds of scopes
global scope
If your file hasn't any import or export line, this file would be executed in global scope that all declaration in it are visible outside this file.
So we would create global variables like this:
// xx.d.ts
declare var age: number
// or
// xx.ts
// with or without declare keyword
var age: number
// other.ts
globalThis.age = 18 // no error
All magic come from
var. Replacevarwithletorconstwon't work.
module scope
If your file has any import or export line, this file would be executed within its own scope that we need to extend global by declaration-merging.
// xx[.d].ts
declare global {
var age: number;
}
// other.ts
globalThis.age = 18 // no error
Answer from edvard chen on Stack OverflowYou can see more about module in official docs
globalThis is the future.
First, TypeScript files have two kinds of scopes
global scope
If your file hasn't any import or export line, this file would be executed in global scope that all declaration in it are visible outside this file.
So we would create global variables like this:
// xx.d.ts
declare var age: number
// or
// xx.ts
// with or without declare keyword
var age: number
// other.ts
globalThis.age = 18 // no error
All magic come from
var. Replacevarwithletorconstwon't work.
module scope
If your file has any import or export line, this file would be executed within its own scope that we need to extend global by declaration-merging.
// xx[.d].ts
declare global {
var age: number;
}
// other.ts
globalThis.age = 18 // no error
You can see more about module in official docs
Inside a .d.ts definition file
type MyGlobalFunctionType = (name: string) => void
If you work in the browser, you add members to the browser's window context:
interface Window {
myGlobalFunction: MyGlobalFunctionType
}
Same idea for NodeJS:
declare module NodeJS {
interface Global {
myGlobalFunction: MyGlobalFunctionType
}
}
Now you declare the root variable (that will actually live on window or global)
declare const myGlobalFunction: MyGlobalFunctionType;
Then in a regular .ts file, but imported as side-effect, you actually implement it:
global/* or window */.myGlobalFunction = function (name: string) {
console.log("Hey !", name);
};
And finally use it elsewhere in the codebase, with either:
global/* or window */.myGlobalFunction("Kevin");
myGlobalFunction("Kevin");
typescript - Call a global variable inside module - Stack Overflow
How to define a global variable in a typescript module, which could be used in other modules directly without import?
How to create a truly global variable in TypeScript without window? - LambdaTest Community
Is it possible to declare a global function/variable type that can be made available in all files part of compilation?
Videos
Hey guys.
Lately, I've been messing around with TypeScript. I come from a full JS background and I wanted to upgrade myself to TS in my spare time.
For context, I've created numerous APIs using Express with JS and I wanted to see if I can recreate those APIs but this time in TS.
However, I've run into a problem: how do you create global variables in TypeScript?
In a NodeJS environment I would normally just go:
// index.js
global.foo = "and I call it a day"
// controller.js
global.foo = "Modified by a different file."
How do I replicate this behavior in TypeScript?
I'm not asking if I should be using Global Variables, or should I reinvent my existing architecture to not use Global Variables; I just need to know how to do it. I've tried endlessly Googling this and while I've come close to finding a solution none of them seem to fit the bill.
For example: http://marcinbiernat.pl/2020/03/nodejs-globals/
This guy just has him declaring things and it works perfectly. The problem I have with it is: I don't want the declarations to clog up index.ts for example. How do I separate my declarations from my code? Is that even possible?
You need to tell the compiler it has been declared:
declare var bootbox: any;
If you have better type information you can add that too, in place of any.
For those who didn't know already, you would have to put the declare statement outside your class just like this:
declare var Chart: any;
@Component({
selector: 'my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.scss']
})
export class MyComponent {
//you can use Chart now and compiler wont complain
private color = Chart.color;
}
In TypeScript the declare keyword is used where you want to define a variable that may not have originated from a TypeScript file.
It is like you tell the compiler that, I know this variable will have a value at runtime, so don't throw a compilation error.