I want to convert an array to type Observable<T[]>
and have used the rxjs method "from". But it returns Observable<T>
, Is there a way to convert an array to Observable<T[]>
?
Player: Observable<T[]>;
convert(){
let tempArr:T[] = Object.values(data) as T[];
let Obsobj = from(tempArr);
this.Player = Obsobj;
}
This is the error message I got,
Observable<T>
is not assignable to type Observable<T[]>
EDIT
Even though using following code snippet return type isn't as expected.
let $sub = new Subject<Array<T>>();
$sub.next(tempArr);
this.Player$ = $sub.asObservable();
Since I want the return observable type to be as follows.
Observable {_isScalar: false, source: Observable, operator: MapOperator}
but returned the following.
Observable {_isScalar: false, source: Subject}
EDIT #2
plete code snippet.
Player: Observable<B[]>;
convert(data: Dictionary<B>){
let tempArr:B[] = Object.values(data) as B[];
let $sub = new Subject<Array<B>>();
$sub.next(tempArr);
this.Player$ = $sub.asObservable();
}
What is the issue here? Any help?
I want to convert an array to type Observable<T[]>
and have used the rxjs method "from". But it returns Observable<T>
, Is there a way to convert an array to Observable<T[]>
?
Player: Observable<T[]>;
convert(){
let tempArr:T[] = Object.values(data) as T[];
let Obsobj = from(tempArr);
this.Player = Obsobj;
}
This is the error message I got,
Observable<T>
is not assignable to type Observable<T[]>
EDIT
Even though using following code snippet return type isn't as expected.
let $sub = new Subject<Array<T>>();
$sub.next(tempArr);
this.Player$ = $sub.asObservable();
Since I want the return observable type to be as follows.
Observable {_isScalar: false, source: Observable, operator: MapOperator}
but returned the following.
Observable {_isScalar: false, source: Subject}
EDIT #2
plete code snippet.
Player: Observable<B[]>;
convert(data: Dictionary<B>){
let tempArr:B[] = Object.values(data) as B[];
let $sub = new Subject<Array<B>>();
$sub.next(tempArr);
this.Player$ = $sub.asObservable();
}
What is the issue here? Any help?
Share Improve this question edited Jul 17, 2019 at 6:40 Ramzan Dieze asked Jul 15, 2019 at 2:58 Ramzan DiezeRamzan Dieze 611 gold badge1 silver badge4 bronze badges3 Answers
Reset to default 7Observable.from Documentation: For arrays and iterables, all contained values will be emitted as a sequence! So your items will always be emitted as T and not T[]
For Observable<T[]>
do either of the following:
$sub = new Subject<Array<T>>();
this.Player = $sub.asObservable();
OR
this.Player = of(array);
OR
Observable.create((observer) => {observer.next(array)})
Use of
operator instead of from
.
import { of } from 'rxjs';
...
player: Observable<T[]>;
convert(){
let tempArr:T[] = Object.values(data) as T[];
let Obsobj = of(tempArr);
this.Player = Obsobj;
}
This isn't how generics work.
You will have to refactor your code in order to make it work.
Ideally your convert
method should expect a generic type T
Something like this:
convert<T>(array: Array<T>): Observable<Array<T>> {
return of(array);
}
And when you call convert
, you call it like this:
this.convert<User>([{ firstName: `John`, lastName: `Doe` }])
.subscribe((userArray: Array<User>) => console.log(userArray));
When you do this, you are telling your convert
function that you want the Type T
to be a User
type.
Here's a Complete working Example:
import { Component } from '@angular/core';
import { Observable, of } from 'rxjs';
interface User {
firstName: string;
lastName: string;
}
@Component({
selector: 'my-app',
templateUrl: './app.ponent.html',
styleUrls: ['./app.ponent.css']
})
export class AppComponent {
player: Observable<Array<User>>;
ngOnInit() {
this.convert<User>([{ firstName: `John`, lastName: `Doe` }])
.subscribe((userArray: Array<User>) => console.log(userArray));
}
convert<T>(array: Array<T>): Observable<Array<T>> {
return of(array);
}
}