Search code examples
angulartypescriptvisual-studio-coderxjscombinelatest

pipe operator not supported with : Angular[Typescript] - rxjs - combineLatest


The Code with the proplem

import { Injectable } from '@angular/core';
import { AccountingTransactionsStoreService } from './accounting-transactions-store.service';
import { GeneralLedgerAccountsStoreService } from './general-ledger-accounts-store.service';
import { distinctUntilChanged, map, combineLatest } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class AccountingReportsStoreService {


  constructor(
    private accountingTransactionsStore: AccountingTransactionsStoreService,
    private   generalLedgerAccountsStore: GeneralLedgerAccountsStoreService) 
    {}

  readonly generalLedgerAccountsTransaction$
    = combineLatest(
      this.accountingTransactionsStore.selectedPeriodAccountingTransactionsFlate$,
      this.generalLedgerAccountsStore.selectedOrganizationGeneralLedgerAccountsTree$)
      .pipe(distinctUntilChanged())
      .pipe(
        map(([transactionsFlat, accountsTree]) => {
          if (transactionsFlat && accountsTree)
            return [];
          else return [];
        })
      )
}

The Error

Property 'pipe' does not exist on type ' OperatorFunction < unknown , [ unknown , AccountingTransactionFlatInterface [ ] , GeneralLedgerAccountInterface [ ] ] > ' .


Solution

  • Whats Wrong?

    import { distinctUntilChanged, map, combineLatest } from 'rxjs/operators';
    

    Why ?

    For real Not sure , but i looks like it's about fiunction defntion Overloads

    Update from ❤ Ingo Burk

    combineLatest exists both as an operator and a function cresting an observable from multiple others. The operator version is deprecated, however. Depending on the import you get one or the other.

    How to solve it

    import combineLatest from 'rxjs' not from 'rxjs/operators'

    import { distinctUntilChanged, map } from 'rxjs/operators';
    import { combineLatest} from 'rxjs';
    

    How did I found the solution?

    • removed the rxjs imports
    • used vscode add all missing imports by accident (I prefare by trial and error )
    • enter image description here

    What I learned ? is there pattern ?

    • yes and no , it's not a pattern , but a trick!🤷‍♀️
    • try to remove imports
    • and use add all missing imports
    • then let VSCode decide what to import
    • then observe the difference
    • then use my jujment.
    • because VSCode has more knowledge of my code in it's memeory than me 🤷‍♀️