Khushi Sharma Khushi Sharma - 20 days ago 5
TypeScript Question

Type 'Observable<{}>' is not assignable to type 'Observable<Item[]>: Rxjs

Here

ItemService

item.service.ts

export const items = (state: any = [], {type, payload}) => {
switch (type) {
default:
return state;
}
};

export interface Item {
id: number;
name: string;
description: string;
};

export interface AppStore {
items: Item[];
selectedItem: Item;
};

@Injectable()
export class ListService {
items: Observable<Array<Item>>;
private listModel: ItemList = new ItemList();

constructor(private itemListApi: ItemListApi, private store: Store<AppStore>) {
this.items = store.select('items');===>Error

}
}


I'm getting an error in
this.items
. The error is "Type 'Observable<{}>' is not assignable to type 'Observable". What went wrong? Any help on this?

Answer

This happens because you defined items as Observable<Array<Item>> while store.select('items') probably returns Observable<T>.

You can use type casting to tell the compiler what you're expecting it to return:

this.items = <Observable<Item[]>>store.select('items');
Comments