【问题标题】:Parameter 'user' implicitly has an 'any' type.ts参数 'user' 隐含一个 'any' type.ts
【发布时间】:2021-04-19 16:18:22
【问题描述】:

我在尝试使用 firebase 设置 Google 身份验证时总是遇到此错误。

参数'user'隐含一个'any'类型.ts(7006)

我到处搜索,但我不知道如何解决它:/

代码:enter code here

user$: Observable<User | null | undefined>;    
async googleSignin() {
        const provider = new firebase.auth.GoogleAuthProvider();
        const credential = await this.afAuth.signInWithPopup(provider);
        return this.updateUserData(credential.user);
      }
    
    
      private updateUserData(user) {    //This user causes the Error
        // Sets user data to firestore on login
        const userRef: AngularFirestoreDocument<User> = this.afs.doc(`users/${user.uid}`);
    
        const data = { 
          uid: user.uid, 
          email: user.email, 
          displayName: user.displayName, 
          photoURL: user.photoURL
          } 
    
        return userRef.set(data, { merge: true })
    
      }

有人可以帮忙吗:D

此代码的文档:https://fireship.io/lessons/angularfire-google-oauth/

【问题讨论】:

  • 给那个参数一个明确的类型?

标签: angular firebase google-authentication


【解决方案1】:

如果您在tsconfig.json 上设置了"noImplicitAny": true,,打字稿将不允许您编写没有类型的表达式或定义。

这里,参数user 没有类型,并且无法从另一个表达式中推断出它的类型。

例如当你正确时

const username = "Tom";

用户名的类型是从“Tom”推断出来的,即string

因此,您应该为用户类型指定any

private updateUserData(user: any) { 
  ....
}

如果您的代码中有任何 User 类型,请使用您的特定类型。

import { User } from 'path/to/types/definitions'

private updateUserData(user: User) { 
  ....
}

【讨论】:

  • 如果它是您要查找的内容,请将其标记为“答案”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-02
  • 1970-01-01
  • 2020-07-30
  • 2023-02-07
  • 2019-08-17
  • 2021-08-22
  • 2021-11-23
相关资源
最近更新 更多