javascript - 返回 promise 在 TypeScript 中给出 "Supplied parameters do not match"

标签 javascript jquery typescript promise

我正在转换 jQuery 插件以使用 promises而不是callbacks 。这应该是非常基本的,但不确定问题是否出在我链接 Promise 的方法、我的返回类型,或者 TypeScript 只是过于挑剔。

我不断收到此错误:

Error   27  Supplied parameters do not match any signature of call target:
    Type '(value: {}) => {}' requires a call signature, but type 'JQueryPromise<any>' lacks one.

我的函数的简化版本如下所示:

    _navigate(forward: boolean, $panel: JQuery, target: string): JQueryPromise<any>
    {
        var dfd = $.Deferred();
        switch (target)
        {
            // '_self' is a full page load link
            case '_self':
                setTimeout(function ()
                {
                    window.location.href = url;
                }, 0);

                // Return empty promise - as we are about to lose the page
                dfd.resolve(true);
                break;
            // _back re-displays a panel already loaded
            case '_back':
                // Fetch a reference to the panel before this one
                var $existingPanel = THIS._getNextPanel($panel, false);

    // >>>> ERROR ON NEXT LINE <<<<<<
                // This will animate back in the previous panel - promise on complete
                dfd = dfd.then(THIS._gotoPanel($existingPanel, false, true, target));

                break;
    // [SNIP]
        }
        return dfd.promise();
    }

_gotoPanel 只是另一种方法,它也返回 JQueryPromise<any> promise :

    _gotoPanel($panel: JQuery, forward: boolean, transition: boolean, target?: string): JQueryPromise<any>

最终版本需要能够链接方法和 $.ajax 中任意数量的 promise 。打电话,但我一直遇到这个错误。如果不是 JQueryPromise<any> 我的返回类型应该是什么样子?

最佳答案

What should my return type look like if not JQueryPromise<any>?

作为错误消息'(value: {}) => {}' requires a call signature说,您需要将回调传递给 .then() ,而不是 promise !

我想你只是想要

dfd = THIS._gotoPanel($existingPanel, false, true, target);

作为dfd否则将根本无法在 _back 中得到解决案例。

关于javascript - 返回 promise 在 TypeScript 中给出 "Supplied parameters do not match",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24362341/

相关文章:

javascript - typescript + RequireJs + AngularJs

javascript - 我想滚动放大和缩小图像,但该图像上的图钉也保持正确定位。查询

javascript - 我应该在 Sencha-Touch 2 中的什么地方放置 Google map 逻辑?多重标记

javascript - 无法同时更改复选框选中状态

javascript - 我的 'body' css 背景图片在 chrome 中跳跃

javascript - 在 table 或 div 元素上使用 hasFocus 绑定(bind)

angular - 将 Observables 与仅一些完成且没有初始值的组合

javascript - 使用angular-cli向Angular中的静态文件添加前缀路径

javascript - 窗口的主干范围而不是这个

javascript - 如何在 js Assets 文件中使用 ruby​​? (未定义方法 'body')