typescript 如何在打字稿中声明全局变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44125398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I declare a global variable in typescript
提问by Jheorge
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'.
我在打字稿中声明了一个全局变量,例如: global.test = "something" 我尝试这样做我得到错误属性 'test' 在类型 'Global' 上不存在。
回答by basarat
I try to do that I get the error property ‘test' does not exist on type ‘Global'.
我尝试这样做,但在类型“全局”上不存在错误属性“测试”。
Create a file globals.d.ts
with
创建一个文件globals.d.ts
与
interface Global {
test: string;
}
More
更多的
Declaration files : https://basarat.gitbook.io/typescript/docs/types/ambient/d.ts.html
声明文件:https: //basarat.gitbook.io/typescript/docs/types/ambient/d.ts.html
回答by nsnze
in global.ts
在 global.ts
export namespace Global {
export var test: string = 'Hello World!';
}
in your.ts
在你的.ts
import { Global } from "./global";
console.log(Global.test)