harsh
harsh

Reputation: 5

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. 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

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

Upvotes: 0

Views: 6956

Answers (1)

Barremian
Barremian

Reputation: 31145

You don't seem to be defining the type of the parameter in tap operator.

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.

Upvotes: 1

Related Questions