【问题标题】:The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?“对象”类型可分配给极少数其他类型。您的意思是改用“任何”类型吗?
【发布时间】:2021-09-30 13:10:54
【问题描述】:

我有一个相当复杂的代码已经运行了两天,我正在使用 rxjs 运算符中的 map 运算符从 IUser 接口中获取 IUser 的值。但我不断收到此错误:

“OperatorFunction”类型的参数不可分配给“OperatorFunction”类型的参数。 “对象”类型可分配给极少数其他类型。您的意思是改用“任何”类型吗? “对象”类型缺少“IUser”类型的以下属性:电子邮件、显示名称、令牌。

提前致谢。

Account.service.ts

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { map } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
import { IUser } from '../shared/models/user';

@Injectable({
  providedIn: 'root'
})
export class AccountService {
  baseUrl = environment.apiUrl;
  private currentUserSource = new BehaviorSubject<IUser>(null!);
  currentUser$ = this.currentUserSource.asObservable();

  constructor(private http: HttpClient, private router: Router) { }

  login(values: any) {
    return this.http.post(this.baseUrl + 'account/login', values).pipe(
      map((user: IUser) => {
        if (user) {
          localStorage.setItem('token', user.token);
          this.currentUserSource.next(user)
        }
      })
    );
  }

login.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}

用户.ts

export interface IUser  {
    email: string;
    displayName: string;
    token: string;
} 

【问题讨论】:

  • 你在哪里订阅 observable?无论如何,map 总是会返回一些东西(在你的代码中没有任何意义)。我怀疑您应该改用tap,或者可能让函数不返回任何内容,并在subscribe 中直接在login 中执行该逻辑。

标签: angular typescript


【解决方案1】:

来自HttpClient post()

重载 #14

post(url: string, body: any | null, options?: {
        headers?: HttpHeaders | {
            [header: string]: string | string[];
        };
        context?: HttpContext;
        observe?: 'body';
        params?: HttpParams | {
            [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
        };
        reportProgress?: boolean;
        responseType?: 'json';
        withCredentials?: boolean;
    }): Observable<Object>;

如果.post() [Overload #14] 不指定T,它将返回Observable&lt;Object&gt; 值。

重载 #15

post<T>(url: string, body: any | null, options?: {
        headers?: HttpHeaders | {
            [header: string]: string | string[];
        };
        context?: HttpContext;
        observe?: 'body';
        params?: HttpParams | {
            [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
        };
        reportProgress?: boolean;
        responseType?: 'json';
        withCredentials?: boolean;
    }): Observable<T>;

post() [Overload #15],您必须指定T,例如post&lt;T&gt;(),这样它将返回Observable&lt;T&gt; 值。


解决方案

假设:返回的响应是IUser类型。

  1. T 指定为IUserany 类型以解决上述错误。

  2. 正如@Gunnar 的评论,你必须使用 tap 而不是 map 从源 observable 执行操作而不改变它

    使用map必须返回Observable的值

login(values: any) {
  return this.http.post<IUser>(this.baseUrl + 'account/login', values).pipe(
    tap((user: IUser) => {
      if (user) {
        localStorage.setItem('token', user.token);
        this.currentUserSource.next(user);
      }
    })
  );
}

【讨论】:

  • 当我使用“tap”时,它仍然返回相同的错误 Type 'Object' is missing the following properties from type 'IUser': email, displayName, token.
  • 您好,请问错误是出自哪一部分?
  • 已经在这个问题上好几天了,但仍然没有消除错误。我仍然需要你的帮助。提前致谢
  • 您能尝试在StackBlitz 上创建最小的、可重现的示例吗?如果不指定所面临问题的更多详细信息,就很难调试和解决问题。
  • 当我尝试使用点击而不是地图时,错误仍然来自“Account.service.ts”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
  • 2021-07-17
相关资源
最近更新 更多