错误 TS2571:对象的类型为“未知”

时间:2021-03-23 07:53:09

标签: angular typescript

Auth-service.ts 正如您在登录和注册方法中看到的那样,我正在尝试存储经过身份验证的用户数据。 我收到一个错误~~~(对象类型未知)

signUp(email:any, password:any){
   return this._http.post<AuthResponse>('https://identitytoolkit.googleapis.com/v1/accounts:signUp?key='+ config.API_KEY, {
      email: email,
      password: password,
      returnSecureToken: true
    }).pipe(
      catchError(err =>{
        return err;
      }),tap(res => {
        this.authenticatedUser(res.email, res.localId, res.idToken , +res.expiresIn) 
      })
    )
  }

  signIn(email:any, password:any){
    return this._http.post<AuthResponse>('https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key='+ config.API_KEY, {
      email: email,
      password: password,
      returnSecureToken: true
    }).pipe(
      catchError(err =>{
        return err;
      }),tap(res =>{
        this.authenticatedUser(res.email, res.localId, res.idToken , +res.expiresIn)
                               ~~~        ~~~          ~~~            ~~~(object is type of unknown)
      })
    )
  }

AuthResponse 接口 正如你所看到的,我创建了一个响应类型的接口......我正在使用 firebase api

export interface AuthResponse {
    idToken: string,
    email: string,
    refreshToken: string,
    expiresIn: string,
    localId: string,
    registered? : string
}

1 个答案:

答案 0 :(得分:1)

您似乎没有在 tap 运算符中定义参数的类型。

signIn(email:any, password:any){
  return this._http.post<AuthResponse>('https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key='+ config.API_KEY, {
    email: email,
    password: password,
    returnSecureToken: true
  }).pipe(
    catchError(err =>{
      return err;
    }),
    tap((res: AuthResponse) => {       // <-- define type here
      this.authenticatedUser(res.email, res.localId, res.idToken , +res.expiresIn)
    })
  );
}

或者您也可以使用括号表示法而不是点表示法。

signIn(email:any, password:any){
  return this._http.post<AuthResponse>('https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key='+ config.API_KEY, {
    email: email,
    password: password,
    returnSecureToken: true
  }).pipe(
    catchError(err =>{
      return err;
    }),
    tap((res: any) => {       
      this.authenticatedUser(res['email'], res['localId'], res['idToken'], +res['expiresIn'])
    })
  );
}

但是由于您已经定义了类型,我建议您使用它而不是 any

相关问题