LOTUSMS
LOTUSMS

Reputation: 10240

Property 'foo' does not exist on type '[number, number, number]'

I'm getting this error

Property 'foo' does not exist on type '[number, number, number]'

I don't understand why or how to fix this. Here's a StackBlitz if you can help

ngOnInit(): void {
   new Observable();

   const foo = of(45);
   const bar = interval(2000);
   const baz = timer(1000);
   // const faz = from(1, 2, 3, 4);

   this.newCombineLatest = combineLatest(foo, bar, baz)
     .pipe(
       tap(res => {
         console.log(this.index++, "foo", res.foo, ", bar: ", res.bar);
       }),
       take(10)
     )
     .subscribe(() => {
       value => console.log(value);
     });
 }

Upvotes: 0

Views: 46

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191729

See the documentation for combineLatest - the Observable output is an array with values that correspond to the latest emission of the ordinal observable inputs.

Arrays are numerically indexed, so res is an array of three numbers. It does not have object keys (except for the length property and array methods, etc.). More to the point, there is no way for JavaScript to know that your variable name is foo and that it should assign a property foo to the array.

Instead, use the numeric indices that correspond to the observable argument order:

console.log(this.index++, "foo", res[0], ", bar: ", res[1]);

Upvotes: 2

Related Questions