什么是 Typescript 中的“声明全局"? [英] What is 'declare global' in Typescript?

查看:35
本文介绍了什么是 Typescript 中的“声明全局"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是声明全局"?这怎么可能?我在 Lodash 类型中找到了这段代码.问题是,当我试图将它复制到 my.d.ts 文件时,因为 global 不是命名空间、模块、函数或 var,我不允许这样做.所以问题是这个声明在 Typescript 中是如何可能的.

What is "declare global" and how is it possible ? I found this code inside Lodash typings. The problem is that when I try to replicate this into my.d.ts file exactly since global is not a namespace, module, function or var, I'm not allowed to do it. So question is how this declaration is possible in Typescript.

附注那么也许有一些额外的编译器选项可以允许这样做?

PS So maybe there are some additional compiler options which will allow this?

declare global {
    interface Set<T> { }
}

推荐答案

这与编译器设置无关.declare global 用于在具有 importexport 的文件中声明全局范围内的事物.这在包含 importexport 的文件中是必要的,因为这些文件被视为模块,并且在模块中声明的任何内容都在模块范围内.

This is not dependent on compiler settings. declare global is used inside a file that has import or export to declare things in the global scope. This is necessary in files that contain import or export since such files are considered modules, and anything declared in a module is in the module scope.

在不是模块的文件(即不包含 import/export)中使用 declare global 是一个错误,因为无论如何,这样的文件都在全局范围内.

Using declare global in a file that is not a module (that is contains no import/export) is an error since everything in such a file is in the global scope anyway.

这篇关于什么是 Typescript 中的“声明全局"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆