12

I declare a global variable in typescript something like: global.test = "something" I try to do that I get the error property ‘test’ does not exist on type ‘Global’.

5
  • I've seen people do it a few different ways. You can setup global variables inside of a .ts file that just exports each variable. Or you can put them in a file that looks like the environment variables file and import that. You'll still have to import the globals file whenever you want to use the variables inside of it, though. Commented May 23, 2017 at 3:40
  • If it is a browser based typescript you can try window.test="something" Commented May 23, 2017 at 3:41
  • 3
    Duplicate of this SO question. Commented May 23, 2017 at 3:56
  • Why does this question (a duplicate of a much better SO entry) show up more and instead of the better one? The only reason I found the original is because @JeroenHeier made that comment. Just the way web search works I guess. :| Commented May 24, 2019 at 18:57
  • This question is similar to: Create a global variable in TypeScript. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Jul 13, 2024 at 22:22

4 Answers 4

18

I try to do that I get the error property ‘test’ does not exist on type ‘Global’.

Create a file globals.d.ts with

interface Global { test: string; } 

More

Declaration files : https://basarat.gitbook.io/typescript/docs/types/ambient/d.ts.html

Sign up to request clarification or add additional context in comments.

3 Comments

export interface? needs export keyword?
This should be at the root of your source folder, also I needed to restart my VSCode for it to work...
That link leads to 'page not found'. This seems to be the new one: basarat.gitbook.io/typescript/type-system/intro/d.ts
8

in global.ts

export namespace Global { export var test: string = 'Hello World!'; } 

in your.ts

import { Global } from "./global"; console.log(Global.test) 

1 Comment

This is not actually global, since the import is still necessary, the answer from @basarat is actually global.
2

Inside a global.d.ts definition file

type MyProfileGlobal = (name: string,age:number) => void 

Config.tsx file

In React:

interface Window { myProfileFun: MyProfileGlobal } 

In NodeJS:

declare module NodeJS { interface Global { myProfileFun: MyProfileGlobal } } 

Now you declare the root variable (that will actually live on window or global)

declare const myProfileFun: MyProfileGlobal; 

Use it elsewhere in code, with either[Add data]:

global/* or window */.myProfileFun("Sagar",28); myProfileFun("Sagar",28); 

Use it elsewhere in code, with either[Get data]:

global/* or window */.myProfileFun= function (name: string,age:number) { console.log("Name: ", name);console.log("Age: ", name); }; 

Comments

0

The simplest way to declare a global variable in typescript:

// foo.ts interface IData { test: string; } declare global { var someVar: IData; } 

Now, in any other file in the project:

// bar.ts import 'foo' somVar.test = 'Yes !!!!' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.