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 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’.
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);
};
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 !!!!'