Javascript jquery TypeScript定义文件中缺少.top?

Javascript jquery TypeScript定义文件中缺少.top?,javascript,jquery,typescript,Javascript,Jquery,Typescript,我正在使用以下代码: $('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top }); Typescript向我发送了一条错误消息,内容是: The property 'top' does not exist on value of type 'Object' 我猜jQuery定义文件中缺少了一些内容。有没有其他人见过这个问题,或者这是jQuery通常不使用的问题?这是我以前从未见过的东西 了解更多信息

我正在使用以下代码:

$('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top });
Typescript向我发送了一条错误消息,内容是:

The property 'top' does not exist on value of type 'Object'
我猜jQuery定义文件中缺少了一些内容。有没有其他人见过这个问题,或者这是jQuery通常不使用的问题?这是我以前从未见过的东西

了解更多信息。以下是使用此选项的代码:

   $.fn.buildTableOfContent = function () {
        "use strict";
        var h2 = this.find('h2');
        if (h2.length > 0) {
            var h1 = this.find('h1:first');
            if (h1.length === 0) {
                h1 = this.prepend('<h1>Help</h1>').children(':first');
            }
            var menu = h1.wrap('<div class="h1 with-menu"></div>')
                    .after('<div class="menu"><img src="/Content/images/menu-open-arrow.png" width="16" height="16"><ul></ul></div>')
                    .next().children('ul');
            h2.each(function (i) {
                this.id = 'step' + i;
                menu.append('<li class="icon_down"><a href="#step' + i + '">' + $(this).html() + '</a></li>');
            });
            menu.find('a').click(function (event) {
                event.preventDefault();
                $('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top });
            });
        }
        return this;
    };
$.fn.buildTableOfContent=函数(){
“严格使用”;
var h2=this.find('h2');
如果(h2.length>0){
var h1=this.find('h1:first');
如果(h1.length==0){
h1=this.prepend('Help')。children(':first');
}
变量菜单=h1.wrap(“”)
.之后(“
    ”) .next().children('ul'); h2.每个(功能(i){ this.id='步骤'+i; menu.append(“
  • ”); }); 菜单。查找('a')。单击(函数(事件){ event.preventDefault(); $('html,body').animate({scrollTop:$($(this.attr('href')).offset().top}); }); } 归还这个; };
    来自jquery.d.ts文件(我的版本中的第374行):

    通过调用不带参数的函数,类型定义期望函数返回
    对象
    类型。我查看了jQuery的文档,您是对的,返回的对象上应该有
    top
    left
    属性

    不幸的是,由于没有基于返回类型的重载,您将无法在
    JQuery
    接口上添加其他成员。因为,在这种特殊情况下,类型定义并不像它应该的那样具体,所以我建议只修改jquery.d.ts文件并更改返回类型,使其看起来如下所示(可能是字符串而不是数字?)

    如果您不想修改此文件,还可以选择在访问其任何属性之前将结果强制转换为
    any
    ,例如:

    $('html, body').animate({ scrollTop: (<any> $($(this).attr('href')).offset()).top });
    
    $('html,body').animate({scrollTop:($($(this.attr('href')).offset()).top});
    
    缺点是,每次调用该函数时,如果没有参数,则需要强制转换。

    而不是此函数

     $('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top });
    
    试试这个

     $('html, body').animate({ scrollTop: $(this).offset().top });
    

    您不能执行
    offset()?:{top:number;left:number;}
    使用TS定义中的
    ?:
    可选项?@mcpDESIGNS我认为它确实有效,但我仍然使用我编辑的jquery.d.TS,它具有修改的声明。无论如何,不,将
    偏移量
    成员设置为可选只意味着不必为了有效实现
    JQuery
    接口而实现它。您似乎无法使用它隐藏成员;如果尝试将其添加到jquery.d.ts文件中,则会阻止
    offset
    的其他重载有效。此外,还需要将其声明为
    offset?:()=>{top:number;left:number;}
    甚至使其在语法上有效。
     $('html, body').animate({ scrollTop: $($(this).attr('href')).offset().top });
    
     $('html, body').animate({ scrollTop: $(this).offset().top });