-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Typescript - .pipe(catchError()) type issues. #3673
Comments
I saw the same thing today. |
I tested this on stackblitz and it doesn't show any compilation error. I tried it also locally with |
@martinsik this is very weird. |
The same here, but the problem seems to be with throwError. public createAccount(name: string, email: string, password: string): Observable<Account> {
return this.http.post<Account>(RequestUtils.getApiUrl('/accounts'), {name, email, password}, RequestUtils.getJsonOptions()).pipe(
map((data: Account) => new Account(data)),
catchError((err: HttpErrorResponse) => {
const apiError = ApiError.withResponse(err);
this.eventService.publish(EventService.EVENT_API_ERROR, apiError);
return Observable.throw(apiError);
})
);
} It compiles but Observable.throw should be replaced by throwError. By replacing, Typescript is forcing me to return the Observable<{} | Account>. public createAccount(name: string, email: string, password: string): Observable<{} | Account> {
return this.http.post<Account>(RequestUtils.getApiUrl('/accounts'), {name, email, password}, RequestUtils.getJsonOptions()).pipe(
map((data: Account) => new Account(data)),
catchError((err: HttpErrorResponse) => {
const apiError = ApiError.withResponse(err);
this.eventService.publish(EventService.EVENT_API_ERROR, apiError);
return throwError(apiError);
})
);
} I don't want to return Observable<{} | Account>, but just Observable< Account >. |
OK, It seems I was importing throwError wrongly. |
|
@benlesh cartSetShippingAddress(sessionId: string, address: Address): Observable<string> {
const url = `...`;
const postData = {...};
return this.http.post(url, postData).pipe(
map((res: {addressId: string}) => res.addressId),
catchError(this.handleError.bind(this))
);
} And in the private handleError(error) {
let errMsg: string;
...
return _throw(errMsg);
} The error that I get is: error TS2322: Type 'Observable<string | {}>' is not assignable to type 'Observable<string>'.
Type 'string | {}' is not assignable to type 'string'.
Type '{}' is not assignable to type 'string'. If I remove the |
@benlesh |
@jdgarvey i have edited your stackblitz to remove the compilation error. You have to typecast your http.get to "MyModel", otherwise it expects something of type "Object", which is not compliant with the methods return type. I also changed it back to the "throwError" method from 'rxjs' (note: no deep import with rxjs @^6.0.0), which i believe (correct me if im wrong) is the correct way to do it:
I am not sure if this will fix the original question as well |
@karstensgit thank you for checking this out! I tried your code, and it fixed the StackBlitz. Then I ported that code over to my project and was still getting an error, but then realized it was because As for proper way to import Thanks again for your help! |
@jdgarvey What TypeScript version you used to compile your example? |
@martinsik My local project is using |
@jdgarvey your example is incorrectly trying to import You're actually not typing the return of this code compiles fine: import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {map, catchError} from 'rxjs/operators';
import {Observable, throwError} from 'rxjs';
interface MyModel {
foo: string;
}
@Injectable()
export class ExampleService {
constructor(private http: HttpClient) {}
exampleGet(): Observable<MyModel> {
return this.http.get<MyModel>('example/endpoint')
.pipe(
catchError(e => throwError(e))
);
}
} |
Given that I haven't seen a minimal reproduction of this issue where some other typing wasn't incorrect, I'm going to close this issue. Here's a minimal version of what people think is going on that does, in fact, compile: import { Observable, of, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
const result: Observable<number> = of(1, 2, 3).pipe(
catchError(err => throwError(new Error('lol'))),
); If you hit this issue, you probably needed to properly type the source going into your |
@benlesh thank you for the example, this works. |
basically there is an issue with catchError because it will mess up with the type resulting in this error like this :
I created a work around this but maybe there is a better way.
When i pass the type to my apiPipe function and to catchError operator no errors comeout but it looks dirty to me.
Anyone have ideas?
The text was updated successfully, but these errors were encountered: