跳到主要内容

在 TypeScript 中声明全局变量

· 阅读需 1 分钟
1adybug
子虚伊人

有时我们需要声明一些全局变量或者模块,此时我们可以使用 declare 方法来实现:

.d.ts 或者 ts 文件中:

declare global {
var tip: string
}
注意
  1. .d.ts 文件中不能有 import 语句,否则它会变成模块
  2. ts 文件必须被引入,或者是入口文件

对于浏览器打包的项目,还可以添加 window 上的变量

declare global {
var tip: string

interface Window {
tip: string
}
}

window.tip = "This is a tip."
注意

必须使用 interface 来扩展声明