-
Notifications
You must be signed in to change notification settings - Fork 3k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strongly type Observable Error #2646
Comments
I disagree. TypeScript does not have error types. |
You could use Observable.throw to help type errors. Promises have the same problem, thats why I created Task |
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 |
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 |
@kyranjamie Strongly typing |
Btw, the equivalent issue in TypeScript is microsoft/TypeScript#13219. If we ever get that issue (declarations for exceptions a function can throw), |
@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 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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 compabilityThe text was updated successfully, but these errors were encountered: