错误TS2345:类型' operatorFunction<对象的参数,对象>'不能分配给类型的参数

发布于 2025-02-13 04:30:53 字数 1086 浏览 1 评论 0原文

我该如何解决这个问题?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝戈者 2025-02-20 04:30:54

关于您指定了返回的函数类型,因为

Observable<{
    token: string;
    userId: Pick<User, "id">;
  }>

您需要在post&lt; t&gt;也是运算符。

Regarding this guide, you specified the returned function type as

Observable<{
    token: string;
    userId: Pick<User, "id">;
  }>

You need to specify the expected returned value type in post<T> and first<T> operators as well.

凉城 2025-02-20 04:30:53

您需要指定post的返回类型,以使其与函数的返回类型匹配。

return this.http.post<{
    token: string;
    userId: Pick<User, "id">;
  }>( ... )
  .pipe( ... )

可以通过编译器来推断管道中的返回类型,但是没有明确说明post的返回类型。

我建议您制作一种类型,以便代码不是那么详细

type TokenAndId = {
    token: string;
    userId: Pick<User, "id">;
  }
  login(
    email: Pick<User, 'email'>,
    password: Pick<User, 'password'>
  ): Observable<TokenAndId> {
    return this.http
      .post<TokenAndId>(
        `${this.url}/login`,
        { email, password },
        this.httpOptions
      )
      .pipe(
        first(),
        tap((tokenObject: TokenAndId) => {
          this.userId = tokenObject.userId;
          localStorage.setItem('token', tokenObject.token);
          this.isUserLoggedIn$.next(true);
          this.router.navigate(['/']);
        }),
        catchError(this.errorHandlerService.handlerError<TokenAndId>('login'))
      );
  }

You need to specify the return type of post so that it matches the return type of the function.

return this.http.post<{
    token: string;
    userId: Pick<User, "id">;
  }>( ... )
  .pipe( ... )

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

type TokenAndId = {
    token: string;
    userId: Pick<User, "id">;
  }
  login(
    email: Pick<User, 'email'>,
    password: Pick<User, 'password'>
  ): Observable<TokenAndId> {
    return this.http
      .post<TokenAndId>(
        `${this.url}/login`,
        { email, password },
        this.httpOptions
      )
      .pipe(
        first(),
        tap((tokenObject: TokenAndId) => {
          this.userId = tokenObject.userId;
          localStorage.setItem('token', tokenObject.token);
          this.isUserLoggedIn$.next(true);
          this.router.navigate(['/']);
        }),
        catchError(this.errorHandlerService.handlerError<TokenAndId>('login'))
      );
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文