错误TS2345:类型' operatorFunction<对象的参数,对象>'不能分配给类型的参数
我该如何解决这个问题?
Angular CLI: 14.0.2
Node: 16.13.0
rxjs: 7.5.5
typescript: 4.7.4
我的导入:
import { Observable, BehaviorSubject } from "rxjs";
import { first, catchError, tap } from "rxjs/operators";
first()上的错误,
login(
email: Pick<User, "email">,
password: Pick<User, "password">
): Observable<{
token: string;
userId: Pick<User, "id">;
}> {
return this.http
.post(`${this.url}/login`, { email, password }, this.httpOptions)
.pipe(
first(),
tap((tokenObject: { token: string; userId: Pick<User, "id"> }) => {
this.userId = tokenObject.userId;
localStorage.setItem("token", tokenObject.token);
this.isUserLoggedIn$.next(true);
this.router.navigate(["/"]);
}),
catchError(
this.errorHandlerService.handlerError<{
token: string;
userId: Pick<User, "id">;
}>("login")
)
);
}
How can I solve this problem?
Angular CLI: 14.0.2
Node: 16.13.0
rxjs: 7.5.5
typescript: 4.7.4
My imports :
import { Observable, BehaviorSubject } from "rxjs";
import { first, catchError, tap } from "rxjs/operators";
Error on first(),
login(
email: Pick<User, "email">,
password: Pick<User, "password">
): Observable<{
token: string;
userId: Pick<User, "id">;
}> {
return this.http
.post(`${this.url}/login`, { email, password }, this.httpOptions)
.pipe(
first(),
tap((tokenObject: { token: string; userId: Pick<User, "id"> }) => {
this.userId = tokenObject.userId;
localStorage.setItem("token", tokenObject.token);
this.isUserLoggedIn$.next(true);
this.router.navigate(["/"]);
}),
catchError(
this.errorHandlerService.handlerError<{
token: string;
userId: Pick<User, "id">;
}>("login")
)
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于您指定了返回的函数类型,因为
您需要在
post&lt; t&gt;
和也是运算符。
Regarding this guide, you specified the returned function type as
You need to specify the expected returned value type in
post<T>
andfirst<T>
operators as well.您需要指定
post
的返回类型,以使其与函数的返回类型匹配。可以通过编译器来推断管道中的返回类型,但是没有明确说明
post
的返回类型。我建议您制作一种类型,以便代码不是那么详细
You need to specify the return type of
post
so that it matches the return type of the function.The return types within the pipe can be inferred by the compiler, but there is no way to tell the return type of
post
without you explicitly stating it.I'd suggest you make a type so the code isn't so verbose