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’.
- 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.Joshua Terrill– Joshua Terrill2017-05-23 03:40:19 +00:00Commented May 23, 2017 at 3:40
- If it is a browser based typescript you can try window.test="something"skvsree– skvsree2017-05-23 03:41:40 +00:00Commented May 23, 2017 at 3:41
- 3Duplicate of this SO question.Jeroen Heier– Jeroen Heier2017-05-23 03:56:54 +00:00Commented 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. :|raddevus– raddevus2019-05-24 18:57:23 +00:00Commented 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.starball– starball ♦2024-07-13 22:22:50 +00:00Commented Jul 13, 2024 at 22:22
Add a comment |
4 Answers
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
3 Comments
Alexander Mills
export interface? needs export keyword?
Rosdi Kasim
This should be at the root of your source folder, also I needed to restart my VSCode for it to work...
Tobias Wicker
That link leads to 'page not found'. This seems to be the new one: basarat.gitbook.io/typescript/type-system/intro/d.ts
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
Rosdi Kasim
This is not actually global, since the import is still necessary, the answer from @basarat is actually global.
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); };