简体   繁体   English

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

[英]error TS2571: Object is of type 'unknown'

Auth-service.ts as you can see in sign in and sign up method I'm trying to store authenticated user data. Auth-service.ts正如您在登录和注册方法中看到的那样,我正在尝试存储经过身份验证的用户数据。 I'm getting an error ~~~(object is type of unknown)我收到一个错误~~~(对象是未知类型)

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-interface as you can see I created an interface of response type... I'm using firebase api如您所见, AuthResponse-interface我创建了一个响应类型的接口...我正在使用 firebase api

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

You don't seem to be defining the type of the parameter in tap operator.您似乎没有在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)
    })
  );
}

Or you could also use bracket notation instead of dot notation.或者您也可以使用括号表示法而不是点表示法。

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'])
    })
  );
}

But since you have already defined the type I'd advise to use it instead of any .但是由于您已经定义了类型,我建议您使用它而不是any

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 TS2571:在 *ngFor 中尝试管道切片时,对象的类型为“未知” - error TS2571: Object is of type 'unknown' when try pipe slice in *ngFor 错误TS2322:无法将类型“对象”分配给类型“接触”。 角 - error TS2322: Type 'Object' is not assignable to type 'Contact'. Angular 错误 TS2322:“对象”类型不可分配给“产品”类型 - Error TS2322: Type 'Object' is not assignable to type 'Produit' 错误 TS2339:“对象”类型上不存在属性“电子邮件” - error TS2339: Property 'email' does not exist on type 'Object' 错误TS2322:类型&#39;对象&#39;不能分配给&#39;电影&#39;类型? Angular 7 - error TS2322: Type 'Object' is not assignable to type 'Movie'? Angular 7 错误TS2345:“对象”类型的参数无法分配给类型的参数 - Error TS2345: Argument of type 'Object' is not assignable to parameter of type 如何解决 Angular 订阅错误? 类型“AngularFireList”上不存在属性“订阅”<unknown> &#39;.ts(2339) - How to solve Angular subscribe error? Property 'subscribe' does not exist on type 'AngularFireList<unknown>'.ts(2339) 错误TS2339:类型“对象”上不存在属性“ json” - error TS2339: Property 'json' does not exist on type 'Object' 错误TS1005:对象类型脚本中应带有新Date()的&#39;(&#39; - Error TS1005: '(' expected in object type script with new Date() 类型“未知”不可分配给类型“键盘事件”.ts (2345) - Type 'unknown' is not assignable to type 'KeyboardEvent'.ts (2345)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM