Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript typing error with forkJoin

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;
        });

The error tsc is emitting is:

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

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?

like image 309
Delmania Avatar asked Sep 26 '26 05:09

Delmania


1 Answers

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];
  })
like image 79
locnguyen Avatar answered Sep 27 '26 20:09

locnguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!