jQuery 3.2.1和TypeScript承诺 [英] jQuery 3.2.1 and TypeScript promises

查看:111
本文介绍了jQuery 3.2.1和TypeScript承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有jQuery 3.2.1的TypeScript 2.4和最新的类型定义。



在一个服务类中,我有这个方法:

  DoSearch(searchItem:string):Promise< IWikipediaRestRecord []> {
return $ .getJSON(https://en.wikipedia.org/w/api.php?action=opensearch&format=json&callback=?&search=+ searchItem)
对于(var i = 0; i< data [1] .length; i ++){
()函数(数据){
var result:IWikipediaRestRecord [] = [];
result.push({
title:data [1] [i],
content:data [2] [i],
link:data [3] [i]
});
}
return result;
});

编译项目,我收到以下错误:

 错误TS2322:键入'** Promise3 **< IWikipediaRestRecord [],永远不会永远不会永远不会永远不会分配键入'** Promise **< IWikipediaRestRecord []>'。 
属性的类型then是不兼容的。
键入'{< ARD = never,AJD = never,AND = never,BRD = never,BJD = never,BND = never,CRD = never,CJD ...'not notable to type' TResult1 = IWikipediaRestRecord [],TResult2 = never>(onfulfilled ?:((value:IWikipediaRestRecord ...'。



我认为这个问题可能是不同类型的承诺
我正在使用ES5编译,并在我的tsconfig.json中包含以下lib:dom,es5,es2015.iterable / p>

任何想法如何解决这个错误?

解决方案

可能最好选择你实际想要使用的承诺实现,本机实现可能是更好的选择,所以你可以这样做

  var jqPromise = $ .getJSON(https://en.wikipedia.org/w/api.php?action=opensearch&format=json&callback=?&search=+ searchItem)
.then( function(data){
var result:IWikipediaRestRecord [] = [];
for(va r i = 0; i result.push({
title:data [1] [i],
content:data [2] [i],
link:data [3] [i]
});
}
返回结果;
});

return Promise.resolve(jqPromise);

最后一行将把jQuery承诺对象转换为标准的ES6承诺。


I am using TypeScript 2.4 with jQuery 3.2.1 and the latest type definitions.

In one service class I have this method:

DoSearch(searchItem: string): Promise < IWikipediaRestRecord[] > {
    return $.getJSON("https://en.wikipedia.org/w/api.php?action=opensearch&format=json&callback=?&search=" + searchItem)
      .then(function(data) {
        var result: IWikipediaRestRecord[] = [];
        for (var i = 0; i < data[1].length; i++) {
          result.push({
            title: data[1][i],
            content: data[2][i],
            link: data[3][i]
          });
        }
        return result;
      });

Compiling the project, I get the following error:

error TS2322: Type '**Promise3**<IWikipediaRestRecord[], any, never, never, never, never, never, never, never>' is not assignable to type '**Promise**<IWikipediaRestRecord[]>'.
  Types of property 'then' are incompatible.
    Type '{ <ARD = never, AJD = never, AND = never, BRD = never, BJD = never, BND = never, CRD = never, CJD...' is not assignable to type '<TResult1 = IWikipediaRestRecord[], TResult2 = never>(onfulfilled?: ((value: IWikipediaRestRecord...'.

I think that the problem can be of different type promises. I am using ES5 compilation and including in my tsconfig.json the following lib: "dom", "es5", "es2015.iterable".

Any idea how can I fix this error?

解决方案

Probably best to pick which promise implementation you actually want to use. The native implementation is probably the better choice, so you can do just that

var jqPromise = $.getJSON("https://en.wikipedia.org/w/api.php?action=opensearch&format=json&callback=?&search=" + searchItem)
  .then(function(data) {
    var result: IWikipediaRestRecord[] = [];
    for (var i = 0; i < data[1].length; i++) {
      result.push({
        title: data[1][i],
        content: data[2][i],
        link: data[3][i]
      });
    }
    return result;
  });

return Promise.resolve(jqPromise);

where that final line will convert the jQuery promise object into a standard ES6 promise.

这篇关于jQuery 3.2.1和TypeScript承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆