窗口是否有打字稿定义

Is there a typescript definition for window?

本文关键字:定义 是否 窗口      更新时间:2023-09-26

我有一个javascript函数,我正在尝试将其转换为打字稿。 以下是函数的一部分:

// needs Markdown.Converter.js at the moment
(function () {
    var util = {},
        position = {},
        ui = {},
        doc = window.document,
        re = window.RegExp,

我收到一个错误,告诉我:属性 RegExp 在类型窗口上不存在。 是否有任何类型的定义文件可以包含在窗口中?

您可以尝试将全局参数传递给匿名块:

(function(window, document) {
  var re = window.RegExp;
  console.log(re);
})(window, document);
Open console...

供参考,您不需要将window用于RegExp,实际上这是不好的做法,因为您无缘无故地将JS环境(节点.js/浏览器)独立代码转换为浏览器特定代码。这就像在节点中使用global.RegExp一样.js当您显然不需要使用 global 时。我会做的:

// needs Markdown.Converter.js at the moment
(function () {
    var util = {},
        position = {},
        ui = {},
        doc = window.document,
        re = RegExp; // No error
})();