我在typescript中声明了一个全局变量,类似于:global。test=“something”我尝试这样做,我得到错误属性“test”在类型“Global”上不存在。
我尝试这样做,我得到错误属性“test”在类型“Global”上不存在。
使用以下内容创建文件< code>globals.d.ts
interface Global {
test: string;
}
声明文件:https://basarat . git book . io/typescript/docs/types/ambient/d . ts . html
在全局中
export namespace Global {
export var test: string = 'Hello World!';
}
在你的.ts
import { Global } from "./global";
console.log(Global.test)
在global.d.ts定义文件中
type MyProfileGlobal = (name: string,age:number) => void
Config.tsx文件
在反应中:
interface Window {
myProfileFun: MyProfileGlobal
}
在 NodeJS 中:
declare module NodeJS {
interface Global {
myProfileFun: MyProfileGlobal
}
}
现在你声明根变量(它实际上存在于窗口或全局变量中)
declare const myProfileFun: MyProfileGlobal;
在代码的其他地方使用它,使用[添加数据]:
global/* or window */.myProfileFun("Sagar",28);
myProfileFun("Sagar",28);
在代码中的其他地方使用它,使用[Get data]:
global/* or window */.myProfileFun= function (name: string,age:number) {
console.log("Name: ", name);console.log("Age: ", name);
};