当 js 用 Typescript 调用 scrollMaxY 参数时,我如何处理

How can I handle when js calls the scrollMaxY param withTypescript?

本文关键字:何处理 处理 js Typescript 调用 参数 scrollMaxY      更新时间:2023-09-26

我正在将代码从javascript转换为在现代浏览器上运行的Typescript。我正在使用VS2013 IDE,除了代码尝试引用scrollMayY的一个地方之外,我没有收到IDE错误。

position.getPageSize = function () {
    var scrollWidth, scrollHeight;
    var innerWidth, innerHeight;
    // It's not very clear which blocks work with which browsers.
    if (self.innerHeight && self.scrollMaxY) {
        scrollWidth = doc.body.scrollWidth;
        scrollHeight = self.innerHeight + self.scrollMaxY;
    }
    else if (doc.body.scrollHeight > doc.body.offsetHeight) {
        scrollWidth = doc.body.scrollWidth;
        scrollHeight = doc.body.scrollHeight;
    }
    else {
        scrollWidth = doc.body.offsetWidth;
        scrollHeight = doc.body.offsetHeight;
    }

在这里我收到一个错误说:属性scrollMaxY在窗口类型的对象上不存在。

谁能给我任何关于我应该如何处理这个问题的建议。我不确定这是代码错误,还是我需要对 Typescript 做一些不同的事情来避免此检查?请注意,代码中的注释不是我写的。这是由代码的开发人员编写的。

您可以在

window上定义scrollMaxY

interface Window { scrollMaxY: number; }

。这将消除您的编译时错误。

或者做:

self["scrollMaxY"]

或者你可以寻找一些替代window.scrollMaxY