0

I want to create an observable from array, but I am getting error (A tuple type element list cannot be empty) every time. I have tried a lot, but still it's not working.

 listdonationsHistory(): Observable<[]> {
     const donationsHistory = [
        {
          id :1,
          type :"Aeri",
          amount: 200,
          frequency: 'monthly',
          date: '17-09-2017',
          account:'ATYTYTYY6778'
        },
        {
          id :2,
          type :"Aeri",
          amount: 200,
          frequency: 'monthly',
          date: '17-09-2017',
          account:'ATYTYTYY6778'
        }
      ];
    return Observable.from(donationsHistory);
 }
4
  • what error do you get? Commented Sep 22, 2017 at 7:02
  • 1
    Try Observable.create(donationsHistory) Commented Sep 22, 2017 at 7:03
  • It's not working, still same error. Commented Sep 22, 2017 at 7:05
  • @kevin, A tuple type element list cannot be empty Commented Sep 22, 2017 at 7:07

2 Answers 2

3

The problem is in the function's signature:

listdonationsHistory(): Observable<[]>

Specifically, it's the return type. That is where the empty tuple is.

To declare the return type as an observable that emits an arbitrary array, you could use:

listdonationsHistory(): Observable<any[]>

However, in the implementation you are calling Observable.from and the returned observable will emit the array elements separately - in which case the return type should be Observable<any>.

If you want an observable that will emit array itself, you should use Observable.of instead of Observable.from.

Sign up to request clarification or add additional context in comments.

4 Comments

Which is more preferred, any[] or Array<any>?
They are the same and different people have differing opinions. I prefer the former, wherever possible.
@cartant, I have tried it also, but it is also not working. And neither I am getting a specific error in this case. I only get runtime error for this.
I've updated the answer - it's not clear whether you want the returned observable to emit the array itself, or the array's elements individually. The answer now explains the difference between calling Observable.from and Observable.of.
1

I can see a problem there. The return type is not an array it is an object. If you want the observable to be an array use Observable.of instead of Observable.from

Try that:

    interface Somereturn {
      id: number;
    }

    listdonationsHistory(): Observable<Somereturn> {
        return Observable.from([{id: 2}, {id: 3}]);
    }

    listdonationsHistory(): Observable<Somereturn[]> {
        return Observable.of([{id: 2}, {id: 3}]);
    }

1 Comment

I have tried that Observable.of also, but didn't get the solution. Bt Yes there is one thing I am not using Interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.