TypeScript files don't know about anything you've done in other TypeScript files unless they have a reference to them. So at the top of bar.ts you should have a line

/// <reference path="foo.ts" />
Answer from Jeffery Grajkowski on Stack Overflow
🌐
TypeScript
typescriptlang.org › docs › handbook › declaration-files › templates › global-d-ts.html
TypeScript: Documentation - Global .d.ts
For example, if you were using jQuery, the $ variable can be used by simply referring to it: ... You’ll usually see guidance in the documentation of a global library of how to use the library in an HTML script tag: ... Today, most popular globally-accessible libraries are actually written as UMD libraries (see below). UMD library documentation is hard to distinguish from global library documentation. Before writing a global declaration file, make sure the library isn’t actually UMD.
Discussions

typescript global variables across files
In nodejs you are able to set /* ... another file after the statement the hello variable is valuable also there. //index.js global.hello = 'hi' require('./other.js') //other.js console.log(hello) //returns hi · Is there an equivalent expression that can achieve the same result in typescript... More on stackoverflow.com
🌐 stackoverflow.com
Typescript Global Variable across Files
I created a variable in a .ts file that has no module or class. It mostly looks just like a plain JavaScript file. I want this variable to accessible in another .ts file inside of a class that is i... More on stackoverflow.com
🌐 stackoverflow.com
November 30, 2017
TypeScript how to declare global variable in every file? - Stack Overflow
Because I have TypeScript linter that catches all unused variables, so you can't just import p and leave it there, it has to be used or deleted. Also, I don't want to put import { p } from xxx in every file. ... I argue that it is a good thing to only import a function when you use it. You, and other maintainer of your project, will get more headaches with global ... More on stackoverflow.com
🌐 stackoverflow.com
How to define a global variable in a typescript module, which could be used in other modules directly without import?
Currently, we have two ways to define a global variable, the first one is use @types define it, and the second one is use declare global in a module. But the first one method need to publish to @ty... More on github.com
🌐 github.com
5
September 4, 2017
🌐
Reddit
reddit.com › r/typescript › is it possible to declare a global function/variable type that can be made available in all files part of compilation?
r/typescript on Reddit: Is it possible to declare a global function/variable type that can be made available in all files part of compilation?
January 28, 2021 -

Currently i'm using js-logger with babel-plugin-js-logger and @babel/plugin-transform-runtime, to make available a global logger function which can be invoked without any explicit imports. Rather we need to initialise the logger only once in the root module, say App.tsx, and it's automatically resolved at runtime by babel.

Currently when i use it in TypeScript, it's leading to linting as well as runtime errors.

This is the function that i use to initialise the logger in logger.ts:

import jsLogger from "js-logger";

const initializeLogger = () => {
  jsLogger.useDefaults();
};

export default initializeLogger;

I invoke the above function once in App.tsx, within its useEffect. So I was thinking of adding a global function declaration for logger, such that TS is also able to understand what this function is all other files where i use say logger.info or logger.error etc.

I though adding the following to App.tsx would make this function available in all files.

import jsLogger from "js-logger";
declare var logger: typeof jsLogger;

But that doesn't seem to be working. Can anyone shed some light on this? What is the correct way to do this?

🌐
Medium
returnofking04.medium.com › typescript-adding-types-for-global-variables-and-functions-5e2cb024f3d6
Typescript: adding types for global variables and functions | by Abhay | Medium
August 28, 2022 - The first solution to the problem can be to declare types for Window interface in your file locally using global declaration. Your declaration of Window will be merged with the global declaration available with typescript and your declared typed ...
🌐
Stack Overflow
stackoverflow.com › questions › 65042019 › typescript-global-variables-across-files
typescript global variables across files
In nodejs you are able to set /* index.js file */ global.hello = 'hello' and if you require another file after the statement the hello variable is valuable also there. //index.js global.hello = 'hi' require('./other.js') //other.js console.log(hello) //returns hi · Is there an equivalent expression that can achieve the same result in typescript?
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-declare-global-variable
How to declare Global Variables in TypeScript | bobbyhadz
To declare a global variable in TypeScript, create a .d.ts file and use declare global{} to extend the global object with typings for the necessary properties or methods.
Find elsewhere
🌐
GitHub
github.com › Microsoft › TypeScript › issues › 18237
How to define a global variable in a typescript module, which could be used in other modules directly without import? · Issue #18237 · microsoft/TypeScript
September 4, 2017 - Currently, we have two ways to define a global variable, the first one is use @types define it, and the second one is use declare global in a module. But the first one method need to publish to @types, which is just for modules written in ...
Author   acrazing
🌐
Marius Schulz
mariusschulz.com › blog › declaring-global-variables-in-typescript
Declaring Global Variables in TypeScript — Marius Schulz
April 16, 2020 - In that file, you can declare all global variables you'll use: ... As long as globals.d.ts is part of your TypeScript project, the compiler will know that __INITIAL_DATA__ is a global variable, and it will let you access it via both __INITIAL_DATA__ as well as window.__INITIAL_DATA__.
🌐
LambdaTest Community
community.lambdatest.com › general discussions
How to create a truly global variable in TypeScript without window? - LambdaTest Community
September 24, 2024 - For example: something = 'testing'; And then use it in another file: if (something === 'testing') In TypeScript, I tried using the window object, like this: In a .d.ts file: interface Window { something: string; } In main.ts: window.something ...
Top answer
1 of 16
176

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. Replace var with let or const won'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

2 of 16
89

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");
🌐
CopyProgramming
copyprogramming.com › howto › typescript-how-to-make-global-variable-in-typescript
Creating a Global Variable in TypeScript: A Guide - Typescript
June 4, 2023 - TypeScript files consist of two types of scopes. When a file does not have any global declarations or references, it is executed in global scope where all declarations are visible outside the file. This allows us to create global variables. On the other hand, if a file has any module declarations ...
🌐
CopyProgramming
copyprogramming.com › howto › typescript-global-variable-across-files
TypeScript Let Variable Declaration, Global Variables Across Files, and 2026 Best Practices - Typescript let variable name any return new typescript fie
January 23, 2026 - Declare ambient types for runtime globals — Use .d.ts files for read-only globals that are injected at runtime. Question: Should I use let or const in TypeScript in 2026? Start with const as your default choice. const prevents accidental reassignment and makes code intent clearer. Only use let when you genuinely need to reassign a variable, which is rare in modern code. Avoid var entirely—it's legacy syntax with unpredictable scoping behavior. Question: How do I share variables across TypeScript files without using true globals?
🌐
Webtips
webtips.dev › solutions › declare-global-variables-in-typescript
3+1 Ways to Declare Global Variables in TypeScript - Webtips
September 20, 2022 - ... If you would like to define all global variables within a single object, you can use the declare global syntax, where you need to define all global variables within a global type.
🌐
Plain English
plainenglish.io › blog › typescript-and-global-variables-in-node-js-59c4bf40cb31
TypeScript and Global Variables in Node.js
August 14, 2021 - Create a file in the root of the project named global.d.ts with the following content. Please note: The use of var , it's required for this to work (see typescriptlang.org for information about this). Without the export {} , all variables will become any
🌐
JavaScript in Plain English
javascript.plainenglish.io › typescript-and-global-variables-in-node-js-59c4bf40cb31
TypeScript and Global Variables in Node.js | by Tomas Nilsson | JavaScript in Plain English
August 16, 2021 - Create a file in the root of the project named global.d.ts with the following content. Please note: The use of var , it’s required for this to work (see typescriptlang.org for information about this). Without the export {} , all variables will become any
🌐
JSDev
jsdev.space › howto › global-vars-ts
How to Use Global Variables in TypeScript Safely
The most reliable way to introduce a global variable is to declare it in a .d.ts file. This file should not contain executable code. Its purpose is to tell TypeScript: “this value exists at runtime; here is its type.” ... Copy Copied!
🌐
Stack Overflow
stackoverflow.com › questions › 60348459 › defining-global-variables-in-typescript-for-wechat-mini-program › 60348616
dom - Defining global variables in TypeScript for WeChat mini program - Stack Overflow
That module is written in multiple TypeScript and is compiled to one minified file. In that module, I need to use DOM/BOM APIs such as document, 'window, 'navigator. On the browser, loading and using the compiled single-file have no problem. But on the WeChat mini-program, there's no global object as on Node.