使用forkJoin的打字稿打字错误 [英] Typescript typing error with forkJoin

查看:113
本文介绍了使用forkJoin的打字稿打字错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用RxJS的forkJoin运算符时,我遇到TypeScript类型检查失败. 这是有问题的代码:

I'm experiencing a TypeScript type check failure when using the forkJoin operator form RxJS. This is the code in question:

let products = this.http.get(productUrl, { headers: this.headers })
            .map(response => response.json().data as Product[]);
        let loans = Observable.of(loan);

        return Observable.forkJoin([products, loans])
            .map(data => {
                let resultProducts = data[0] as Product[];
                if (resultProducts.length > 0) {
                    lead.Product = resultProducts.find(i => i.ID == productId);
            }
            lead.Loan = data[1];
            return lead;
        });

tsc发出的错误是:

The error tsc is emitting is:

 Type 'Loan' cannot be converted to type 'Product[]'.

我对forkJoin的理解是data [0]应该是Product [],data [1]应该是贷款,但是tsc似乎不同意.我的理解不正确吗?我是否错过了一些可以告诉我想要的tsc帽子的输入?

My understanding of forkJoin is that data[0] should be a Product[] and data[1] should be a Loan, but tsc seems to disagree. Is my understanding incorrect? Am I missing some typing that would tell tsc hat I want?

推荐答案

尝试在不传递数组的情况下使用Observable.forkJoin.

Try using Observable.forkJoin without passing in an array.

// forkJoin with array
return Observable.forkJoin([products, loans])
// forkJoin without array
return Observable.forkJoin(products, loans)
  .map(data => {
    let resultProducts = data[0] as Product[];
    // ...
    lead.Loan = data[1];
  })

这篇关于使用forkJoin的打字稿打字错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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