Strongly type Observable Error #6328
Replies: 8 comments
-
I disagree. TypeScript does not have error types. |
Beta Was this translation helpful? Give feedback.
-
You could use Observable.throw to help type errors. Promises have the same problem, thats why I created Task |
Beta Was this translation helpful? Give feedback.
-
Here is a use case I often run into: put<T>(path: string, body = {}): Observable<T> {
return this.http
.put<T>(`${environment.apiUrl}${path}`, JSON.stringify(body), {
headers: this.setHeaders(),
}).pipe(
catchError((err: HttpErrorResponse) => throwError(new DomainError(err.error.message)))
);
} In this example code the intention is that I want to translate the HTTP error to my specific domain error. The caller of the Also note that I had to type the error as |
Beta Was this translation helpful? Give feedback.
-
Was about to make an issue, but this seems close enough... It'd be great if Our use case is simple. A trading platform where an order can either succeed or fail. If it fails, we want to terminate the stream, update the UI etc. Consider this example: // order.service.ts
submitOrder (order) { // implicitly Observable<Order>
return this.http.post<Order>(order).pipe(
// switch to stream of order events
switchMap(orderId => this.orderEvents$.pipe(filterById(orderId)),
// throw error if order fails
switchMap(order => order.status === 'error' ? throwError(order) : of(order))
);
} // some-component.ts
this.service.submitOrder(order).subscribe(
successfulOrder => { ... }, // implictly type as `Order`
failedOrder => { ... } // typed as `any`
); Were switchMap(order => order.status === 'error' ? throwError<Order>(order) : of(order)) ...we would have implicit typing. Without the need to coerce the type in each case of |
Beta Was this translation helpful? Give feedback.
-
@kyranjamie Strongly typing |
Beta Was this translation helpful? Give feedback.
-
Btw, the equivalent issue in TypeScript is microsoft/TypeScript#13219. If we ever get that issue (declarations for exceptions a function can throw), |
Beta Was this translation helpful? Give feedback.
-
@felixfbecker This is an interesting point, however the developer can assert the type of observable errors and promise rejections, yet cannot specify these assertions as type parameters, while TypeScript could enforce that type contract in subscribers. TS cannot enforce that type on the provider side, but do two wrongs make it right? I understand that the final error is a union of all possible errors in the chain (whether it's JS stack, Promise chain, Observable operator chain), so probably every |
Beta Was this translation helpful? Give feedback.
-
Figured out this pattern for Angular: declare class ErrorObservable<T, U = any> {
subscribe(
next?: (value: T) => void,
error?: (error: { error: U }) => void,
complete?: () => void
): Subscription;
}
// Example definition in service
deleteUser(id: string) {
return this.http.delete('/user') as ErrorObservable<SuccessType, ErrorType>;
}
// Example usage in component
deleteUser('abc').subscribe(a => { /* a is of SuccessType */ }, b => { /* b.error is of ErrorType */ } |
Beta Was this translation helpful? Give feedback.
-
RxJS version: [email protected]
Code to reproduce: none
Expected behavior:
Observable<T,E>
should be used for definition whereE
tell type of error to be expectedActual behavior:
Observable<T>
where error is typed asany
Additional information: Typescript 2.3 provide default type for generics using that we can have
Observable<T,E=any>
for backward compabilityBeta Was this translation helpful? Give feedback.
All reactions