Why map doesn't work after upgrading RxJS to v6.3
up vote
0
down vote
favorite
I have a similar case to one described in this post.
I have a user login service, which (among other things) verifies if user's token is still valid. The server's response is defined in an interface:
export interface UserVerifyResponse {
success: boolean
}
My aim was to create an observable that will return a boolean value depending on whether user is verified. This code was working with RxJS v6.2:
authenticate(): Observable<boolean> {
return this.http.get<boolean>(
this.apiUrl+'/verify_user'
).pipe(
map<UserVerifyResponse, boolean>((receivedData: UserVerifyResponse) => {
return receivedData.success;
}),
tap((data: boolean) => {console.log("User authenticated", data)}),
catchError(this.handleError)
)
}
However, now that I have updated RxJS to v6.3 I get this error:
ERROR in src/app/login/user.service.ts(50,13): error TS2345: Argument of type 'OperatorFunction<UserVerifyResponse, boolean>' is not assignable to parameter of type 'OperatorFunction<boolean, boolean>'.
Type 'UserVerifyResponse' is not assignable to type 'boolean'.
It bothers me, because I use this approach of mapping API response to an internal class or a primitive (in other place I have a service which uses http.get<T>
) and now I wonder if I should force RxJS 6.2 or there is an easy way to migrate to 6.3. I can rewrite all of them as it is described in the answer to the above mentioned post, but I I want to return a boolean value and my approach looks clearer in my opinion.
Any suggestions?
typescript type-conversion rxjs rxjs6 rxjs-pipeable-operators
add a comment |
up vote
0
down vote
favorite
I have a similar case to one described in this post.
I have a user login service, which (among other things) verifies if user's token is still valid. The server's response is defined in an interface:
export interface UserVerifyResponse {
success: boolean
}
My aim was to create an observable that will return a boolean value depending on whether user is verified. This code was working with RxJS v6.2:
authenticate(): Observable<boolean> {
return this.http.get<boolean>(
this.apiUrl+'/verify_user'
).pipe(
map<UserVerifyResponse, boolean>((receivedData: UserVerifyResponse) => {
return receivedData.success;
}),
tap((data: boolean) => {console.log("User authenticated", data)}),
catchError(this.handleError)
)
}
However, now that I have updated RxJS to v6.3 I get this error:
ERROR in src/app/login/user.service.ts(50,13): error TS2345: Argument of type 'OperatorFunction<UserVerifyResponse, boolean>' is not assignable to parameter of type 'OperatorFunction<boolean, boolean>'.
Type 'UserVerifyResponse' is not assignable to type 'boolean'.
It bothers me, because I use this approach of mapping API response to an internal class or a primitive (in other place I have a service which uses http.get<T>
) and now I wonder if I should force RxJS 6.2 or there is an easy way to migrate to 6.3. I can rewrite all of them as it is described in the answer to the above mentioned post, but I I want to return a boolean value and my approach looks clearer in my opinion.
Any suggestions?
typescript type-conversion rxjs rxjs6 rxjs-pipeable-operators
By thisthis.http.get<boolean>
you're saying that this request will return an Observable emittingboolean
s. But then you usemap<UserVerifyResponse, boolean>
whereUserVerifyResponse
is an object instead ofboolean
.
– martin
Nov 10 at 15:06
Because this is what I want the request to return. With RxJS v6.2 I was able to translate the response that I received from the API (of typeUserVerifyResponse
) toboolean
, so finally thehttp.get
would return Observable of typeboolean
. Why was it acceptable previously and now it is wrong logic?
– george007
Nov 10 at 15:23
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a similar case to one described in this post.
I have a user login service, which (among other things) verifies if user's token is still valid. The server's response is defined in an interface:
export interface UserVerifyResponse {
success: boolean
}
My aim was to create an observable that will return a boolean value depending on whether user is verified. This code was working with RxJS v6.2:
authenticate(): Observable<boolean> {
return this.http.get<boolean>(
this.apiUrl+'/verify_user'
).pipe(
map<UserVerifyResponse, boolean>((receivedData: UserVerifyResponse) => {
return receivedData.success;
}),
tap((data: boolean) => {console.log("User authenticated", data)}),
catchError(this.handleError)
)
}
However, now that I have updated RxJS to v6.3 I get this error:
ERROR in src/app/login/user.service.ts(50,13): error TS2345: Argument of type 'OperatorFunction<UserVerifyResponse, boolean>' is not assignable to parameter of type 'OperatorFunction<boolean, boolean>'.
Type 'UserVerifyResponse' is not assignable to type 'boolean'.
It bothers me, because I use this approach of mapping API response to an internal class or a primitive (in other place I have a service which uses http.get<T>
) and now I wonder if I should force RxJS 6.2 or there is an easy way to migrate to 6.3. I can rewrite all of them as it is described in the answer to the above mentioned post, but I I want to return a boolean value and my approach looks clearer in my opinion.
Any suggestions?
typescript type-conversion rxjs rxjs6 rxjs-pipeable-operators
I have a similar case to one described in this post.
I have a user login service, which (among other things) verifies if user's token is still valid. The server's response is defined in an interface:
export interface UserVerifyResponse {
success: boolean
}
My aim was to create an observable that will return a boolean value depending on whether user is verified. This code was working with RxJS v6.2:
authenticate(): Observable<boolean> {
return this.http.get<boolean>(
this.apiUrl+'/verify_user'
).pipe(
map<UserVerifyResponse, boolean>((receivedData: UserVerifyResponse) => {
return receivedData.success;
}),
tap((data: boolean) => {console.log("User authenticated", data)}),
catchError(this.handleError)
)
}
However, now that I have updated RxJS to v6.3 I get this error:
ERROR in src/app/login/user.service.ts(50,13): error TS2345: Argument of type 'OperatorFunction<UserVerifyResponse, boolean>' is not assignable to parameter of type 'OperatorFunction<boolean, boolean>'.
Type 'UserVerifyResponse' is not assignable to type 'boolean'.
It bothers me, because I use this approach of mapping API response to an internal class or a primitive (in other place I have a service which uses http.get<T>
) and now I wonder if I should force RxJS 6.2 or there is an easy way to migrate to 6.3. I can rewrite all of them as it is described in the answer to the above mentioned post, but I I want to return a boolean value and my approach looks clearer in my opinion.
Any suggestions?
typescript type-conversion rxjs rxjs6 rxjs-pipeable-operators
typescript type-conversion rxjs rxjs6 rxjs-pipeable-operators
asked Nov 10 at 15:00
george007
339212
339212
By thisthis.http.get<boolean>
you're saying that this request will return an Observable emittingboolean
s. But then you usemap<UserVerifyResponse, boolean>
whereUserVerifyResponse
is an object instead ofboolean
.
– martin
Nov 10 at 15:06
Because this is what I want the request to return. With RxJS v6.2 I was able to translate the response that I received from the API (of typeUserVerifyResponse
) toboolean
, so finally thehttp.get
would return Observable of typeboolean
. Why was it acceptable previously and now it is wrong logic?
– george007
Nov 10 at 15:23
add a comment |
By thisthis.http.get<boolean>
you're saying that this request will return an Observable emittingboolean
s. But then you usemap<UserVerifyResponse, boolean>
whereUserVerifyResponse
is an object instead ofboolean
.
– martin
Nov 10 at 15:06
Because this is what I want the request to return. With RxJS v6.2 I was able to translate the response that I received from the API (of typeUserVerifyResponse
) toboolean
, so finally thehttp.get
would return Observable of typeboolean
. Why was it acceptable previously and now it is wrong logic?
– george007
Nov 10 at 15:23
By this
this.http.get<boolean>
you're saying that this request will return an Observable emitting boolean
s. But then you use map<UserVerifyResponse, boolean>
where UserVerifyResponse
is an object instead of boolean
.– martin
Nov 10 at 15:06
By this
this.http.get<boolean>
you're saying that this request will return an Observable emitting boolean
s. But then you use map<UserVerifyResponse, boolean>
where UserVerifyResponse
is an object instead of boolean
.– martin
Nov 10 at 15:06
Because this is what I want the request to return. With RxJS v6.2 I was able to translate the response that I received from the API (of type
UserVerifyResponse
) to boolean
, so finally the http.get
would return Observable of type boolean
. Why was it acceptable previously and now it is wrong logic?– george007
Nov 10 at 15:23
Because this is what I want the request to return. With RxJS v6.2 I was able to translate the response that I received from the API (of type
UserVerifyResponse
) to boolean
, so finally the http.get
would return Observable of type boolean
. Why was it acceptable previously and now it is wrong logic?– george007
Nov 10 at 15:23
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Apparently, they have improved the type-checking.
When you wrote this.http.get<boolean>
, you are saying "this get is returning an Observable of type boolean", which is not what you mean. The get is returning an Observable of type UserVerifyResponse
and you should say so:
authenticate(): Observable<boolean> {
return this.http.get<UserVerifyResponse>(
this.apiUrl+'/verify_user'
).pipe(
map((receivedData) => {
return receivedData.success;
}),
tap((data) => {console.log("User authenticated", data)}),
catchError(this.handleError)
)
}
The pipe alters the Observable from UserVerifyResponse
to the boolean
that is ultimately returned.
Note that I have deleted most of the typing you had. In general, you should only specify type when:
- you have to, as is the case with the
get()
itself, since the TypeScript compiler cannot correctly infer the type, or
- you are writing a publicly available function, as is the case with
authenticate()
, since while TypeScript can infer the type, someone reading your code later probably cannot.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Apparently, they have improved the type-checking.
When you wrote this.http.get<boolean>
, you are saying "this get is returning an Observable of type boolean", which is not what you mean. The get is returning an Observable of type UserVerifyResponse
and you should say so:
authenticate(): Observable<boolean> {
return this.http.get<UserVerifyResponse>(
this.apiUrl+'/verify_user'
).pipe(
map((receivedData) => {
return receivedData.success;
}),
tap((data) => {console.log("User authenticated", data)}),
catchError(this.handleError)
)
}
The pipe alters the Observable from UserVerifyResponse
to the boolean
that is ultimately returned.
Note that I have deleted most of the typing you had. In general, you should only specify type when:
- you have to, as is the case with the
get()
itself, since the TypeScript compiler cannot correctly infer the type, or
- you are writing a publicly available function, as is the case with
authenticate()
, since while TypeScript can infer the type, someone reading your code later probably cannot.
add a comment |
up vote
0
down vote
accepted
Apparently, they have improved the type-checking.
When you wrote this.http.get<boolean>
, you are saying "this get is returning an Observable of type boolean", which is not what you mean. The get is returning an Observable of type UserVerifyResponse
and you should say so:
authenticate(): Observable<boolean> {
return this.http.get<UserVerifyResponse>(
this.apiUrl+'/verify_user'
).pipe(
map((receivedData) => {
return receivedData.success;
}),
tap((data) => {console.log("User authenticated", data)}),
catchError(this.handleError)
)
}
The pipe alters the Observable from UserVerifyResponse
to the boolean
that is ultimately returned.
Note that I have deleted most of the typing you had. In general, you should only specify type when:
- you have to, as is the case with the
get()
itself, since the TypeScript compiler cannot correctly infer the type, or
- you are writing a publicly available function, as is the case with
authenticate()
, since while TypeScript can infer the type, someone reading your code later probably cannot.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Apparently, they have improved the type-checking.
When you wrote this.http.get<boolean>
, you are saying "this get is returning an Observable of type boolean", which is not what you mean. The get is returning an Observable of type UserVerifyResponse
and you should say so:
authenticate(): Observable<boolean> {
return this.http.get<UserVerifyResponse>(
this.apiUrl+'/verify_user'
).pipe(
map((receivedData) => {
return receivedData.success;
}),
tap((data) => {console.log("User authenticated", data)}),
catchError(this.handleError)
)
}
The pipe alters the Observable from UserVerifyResponse
to the boolean
that is ultimately returned.
Note that I have deleted most of the typing you had. In general, you should only specify type when:
- you have to, as is the case with the
get()
itself, since the TypeScript compiler cannot correctly infer the type, or
- you are writing a publicly available function, as is the case with
authenticate()
, since while TypeScript can infer the type, someone reading your code later probably cannot.
Apparently, they have improved the type-checking.
When you wrote this.http.get<boolean>
, you are saying "this get is returning an Observable of type boolean", which is not what you mean. The get is returning an Observable of type UserVerifyResponse
and you should say so:
authenticate(): Observable<boolean> {
return this.http.get<UserVerifyResponse>(
this.apiUrl+'/verify_user'
).pipe(
map((receivedData) => {
return receivedData.success;
}),
tap((data) => {console.log("User authenticated", data)}),
catchError(this.handleError)
)
}
The pipe alters the Observable from UserVerifyResponse
to the boolean
that is ultimately returned.
Note that I have deleted most of the typing you had. In general, you should only specify type when:
- you have to, as is the case with the
get()
itself, since the TypeScript compiler cannot correctly infer the type, or
- you are writing a publicly available function, as is the case with
authenticate()
, since while TypeScript can infer the type, someone reading your code later probably cannot.
answered Nov 10 at 16:56
Malvolio
24k2179107
24k2179107
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240192%2fwhy-map-doesnt-work-after-upgrading-rxjs-to-v6-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
By this
this.http.get<boolean>
you're saying that this request will return an Observable emittingboolean
s. But then you usemap<UserVerifyResponse, boolean>
whereUserVerifyResponse
is an object instead ofboolean
.– martin
Nov 10 at 15:06
Because this is what I want the request to return. With RxJS v6.2 I was able to translate the response that I received from the API (of type
UserVerifyResponse
) toboolean
, so finally thehttp.get
would return Observable of typeboolean
. Why was it acceptable previously and now it is wrong logic?– george007
Nov 10 at 15:23