0
votes

I'm coming back to an Angular project after a long while away, and I'm updating Angular (and RxJS) to version 6. One of the many errors being thrown relate (I assume) to pipeable operators, which were new in RxJS 6. Here's an example of one:

src/app/member-view/member-view.service.ts(126,13): error TS2339: Property 'monthOne' does not exist on type 'any[]'.

Here's the code that throws it:

let results = this.db.list(`renown/${uid}/${this.years[0]}/${this.months[0]}/total`)
.pipe(switchMap(total => {
    let joinedObservables: any[] = []
    joinedObservables.push(this.db
        .object(`renown/${uid}/${this.years[0]}/${this.months[0]}/total`)
        .pipe(tap(value => {
            if (value.$value !== null) {
                total.monthOne = value.$value

This error did not exist until the operator switchMap was moved to within the .pipe(), which is required as of RxJS 6. Is this a typing issue or a syntax error?

1
Apparently the $value atribute of the result of this.db.object(...) is an array. Maybe you want to use value.$value[0]... - julianobrasil
total is actually an int. I'm realizing that I'm assigning a property to total here, the assigning a value to that. Can I not assign a property this way anymore? - J. Adam Connor
Yeah, I updated my comment right after adding it (I've noticed I was referring to the wrong part of the code). Based on my updated comment above, it worth console.log(value.$value) - julianobrasil
It's just javascript, you can do whatever you want most of the time. Creating a property on total is not an issue (I find it particularly confusing in the long term). - julianobrasil
Thing is, if I remove the .pipe(switchMap...) there's no error. Except it doesn't recognize switchMap anymore because with RxJS you have to pipe operators now. And the code won't compile, so I can't log in the console. - J. Adam Connor

1 Answers

0
votes

You should type your data all the way. Since you haven't typed your data, this.db.list apparently gives you any[], since it's not typed. So type your data all the way by for example using interfaces, here I am using MyType1> and MyType2>:

let results = this.db.list<MyType1>(`renown/${uid}/${this.years[0]}/${this.months[0]}/total`)
.pipe(switchMap((total: MyType1) => {
    let joinedObservables: MyType[] = []
    joinedObservables.push(this.db
        .object<MyType2>(`renown/${uid}/${this.years[0]}/${this.months[0]}/total`)
        .pipe(tap((value: MyType2) => {
            if (value.$value !== null) {
                total.monthOne = value.$value