Here's an approach. I don't know if this is the 'correct' way of doing things, but it works for me with TypeScript 3.7.4.
- Assuming your source files live in a folder
src, create a new foldersrc/typesand create a fileglobal.d.tsin this folder. - Author your declarations using one of the following strategies:
- If you need to import external types into your declaration file, use the following syntax:
import { Express } from 'express';
declare global {
namespace NodeJS {
interface Global {
__EXPRESS_APP__: Express;
}
}
}
- If your declaration file does not contain any imports, the above will not work, and you'll need to use this syntax instead:
declare namespace NodeJS {
interface Global {
__CONNECTION_COUNT__: number;
}
}
- Make sure your
global.d.tsfile (and any other files you might add tosrc/types) is picked up by the TypeScript compiler, by adding the following to yourtsconfig.jsonfile:
{
"paths": {
"*": ["node_modules/*", "src/types/*"]
}
}
- Use the global variable as normal inside your code.
// Below, `app` will have the correct typings
const app = global.__EXPRESS_APP__;
Answer from Tom Spencer on Stack OverflowHere's an approach. I don't know if this is the 'correct' way of doing things, but it works for me with TypeScript 3.7.4.
- Assuming your source files live in a folder
src, create a new foldersrc/typesand create a fileglobal.d.tsin this folder. - Author your declarations using one of the following strategies:
- If you need to import external types into your declaration file, use the following syntax:
import { Express } from 'express';
declare global {
namespace NodeJS {
interface Global {
__EXPRESS_APP__: Express;
}
}
}
- If your declaration file does not contain any imports, the above will not work, and you'll need to use this syntax instead:
declare namespace NodeJS {
interface Global {
__CONNECTION_COUNT__: number;
}
}
- Make sure your
global.d.tsfile (and any other files you might add tosrc/types) is picked up by the TypeScript compiler, by adding the following to yourtsconfig.jsonfile:
{
"paths": {
"*": ["node_modules/*", "src/types/*"]
}
}
- Use the global variable as normal inside your code.
// Below, `app` will have the correct typings
const app = global.__EXPRESS_APP__;
I found this works.
Have one file that declares the property on the NodeJS.Global interface with the any type. This file has to be clean of imports or refrences.
node.d.ts
declare namespace NodeJS{
interface Global {
foo: any
}
}
Then in the second file you declare a global variable that has the correct type.
global.d.ts
import IFoo from '../foo'
declare global {
const foo:Ifoo
}
Extending TypeScript Global object in node.js - Stack Overflow
Global Variables
typescript - Declare global variable in NodeJS - Stack Overflow
global variables in nodejs
You probably shouldn't be using a global variable for what you're trying to do, but…
to let TypeScript know about your own properties on the global object you need to augment the Global interface in a d.ts file:
export interface Global {
myProp: string;
}If all you want is to 'share' stuff across files (i.e. modules) you probably want to export from your modules rather than add to the global object.
More on reddit.comVideos
As of node@16 the NodeJS.Global interface has been removed in favor of globalThis.
You can declare new global variable in a module file as:
declare global {
var NEW_GLOBAL: string;
}
And in a non-module file (no top-level import/export) as:
declare var NEW_GLOBAL: string;
Important note: variable must be declared as var. let and const variables don't show up on globalThis.
I saw this SO post and tried this:
You probably have something like vendor.d.ts:
// some import
// AND/OR some export
declare module NodeJS {
interface Global {
spotConfig: any
}
}
Your file needs to be clean of any root level import or exports. That would turn the file into a module and disconnect it from the global type declaration namespace.
More : https://basarat.gitbooks.io/typescript/content/docs/project/modules.html
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?
I'm trying to add a global variable in nodejs but it doesn't find them in other files, I have 2 problems
-
Typescript doesn't let me use
global.varname, onlyglobal['varname']. -
How do I declare the variable in another file?
You probably shouldn't be using a global variable for what you're trying to do, but…
to let TypeScript know about your own properties on the global object you need to augment the Global interface in a d.ts file:
export interface Global {
myProp: string;
}
If all you want is to 'share' stuff across files (i.e. modules) you probably want to export from your modules rather than add to the global object.
The language spec has a lot to say about ambient declarations using the keyword 'declare' in section 1.1
I'm pretty sure 'declare var myThing' is what you are looking for. I could also be wrong.
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");