Here is a Typescript class I am working with:
export class Envelope<T> {
result: T;
constructor(result: T) {
this.result = result;
}
}
I'm trying to convert
Envelope<RecentPostResponse[]>
to Observable<PostModel[]>
:
getPosts(): Observable<PostModel[]> {
return this.postService.getRecent().pipe(
map((envelope: Envelope<RecentPostResponse[]>) =>
envelope.result.map((response: RecentPostResponse) => {
return {
id: response.id,
// Other properties
};
})));
However, I encounter the following error:
Argument of type 'OperatorFunction<Envelope<RecentPostResponse[]>, { id: number; }[]>'.
Type 'Envelope<RecentPostResponse[]>' is not assignable to type 'Envelope<RecentPostResponse>'.
What am I missing?
Update:
The original envelope had its result type as an array (result type was T[]
):
export class Envelope<T> {
result: T[];
constructor(result: T[]) {
this.result = result;
}
}
Initially, the conversion was working as expected:
getPosts(): Observable<PostModel[]> {
return this.postService.getRecent().pipe(
map((envelope: Envelope<RecentPostResponse>) =>
envelope.result.map((response: RecentePostResponse) => {
return {
id: response.id,
};
})));
};
Then I made a change to Envelope where the result type changed from T[] to T:
export class Envelope<T> {
result: T;
constructor(result: T) {
this.result = result;
}
}
But now I am struggling to modify the conversion accordingly...