带有 $(document).ready 的打字稿 3.0

Typescript 3.0 with $(document).ready

在Typescript中订阅jQuery document.ready()的一般描述方法似乎如下

class Foo {
    constructor() {
        jQuery(document).ready(() => {
           ...
        });
    }
}

但是在 Typescript 3.0 中我遇到了一个错误..

Supplied parameters do not match any signature of call type...
should have type assignment to string...
but has type 'Document'

这是错误还是正确的签名是什么。

答案取自

$(document).ready(handler)有两个功能等价的变体,第一个是$().ready(handler),第二个是直接$(handler).

在 jQuery 3.0 中,前两个已弃用,仅保留 $(handler)。官方给出的理由是:

This is because selection has no bearing on the behavior of the .ready() method, which is inefficient and can lead to incorrect assumptions about the method's behavior.

TypeScript 定义文件只是不包含已弃用的语法,为了向后兼容,这些语法仍然有效。您的脚本应如下所示:

$(() => {
    console.log("Hello World!");
});