I have a type I want to return from a method that is not the same as one the http request gets - I basically want to assign the results of that http request as a property on the object and return an observable of that object. I understand why the below is not going to work but for the purposes of having code..
getAuditsByObjectAndType<T>(object: T): Observable<IAuditInformation<T>> {
const auditInfo: IAuditInformation<T> = {
audits: [],
object: object
};
auditInfo.audits = this.get<IAudit>(`audits?id=${object.id}`)
return Observable.of(auditInfo)
}
Is there an operator that will help me do what I want please?
Edit:
Got a working implementation but probably could be a lot better..
getAuditsByObjectAndType<T>(object: T): Observable<IAuditInformation<T>> {
const auditInfo: Observable<IAuditInformation<T>> = Observable.of({
audits: [],
object: object
});
return this.get<IAudit>(`AuditItems?id=${object.id}`)
.combineLatest(auditInfo, (audits, info) => {
info.audits = audits;
return info
});
}
You could map the response:
import 'rxjs/add/operator/map';
getAuditsByObjectAndType<T>(object: T): Observable<IAuditInformation<T>> {
return this.get<IAudit>(`audits?id=${object.id}`)
.map(audits => ({
audits,
object
}));
}
However typescript has no way of knowing that your T
contains an id
property, to solve that you could do the following:
interface TheObjectWithAnId {
id: number;
}
getAuditsByObjectAndType<T extends TheObjectWithAnId>(object: T): Observable<IAuditInformation<T>>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.