Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在react中为firebase对象编写类型定义?_Reactjs_Typescript_Firebase_React Native - Fatal编程技术网

Reactjs 如何在react中为firebase对象编写类型定义?

Reactjs 如何在react中为firebase对象编写类型定义?,reactjs,typescript,firebase,react-native,Reactjs,Typescript,Firebase,React Native,我正在尝试将firebase与react native(expo)集成。我遵循找到的firebase文档。文档非常直截了当,并按预期工作,但由于文档是JS格式的,并且我正在使用typescript进行编码,因此我的linter上出现了不少错误 关于下面的代码,我想谈几点 我已经成功地定义了AuthenticatedUser,因为当我在signInWithGooglAsync中调用方法onSignIn(result)时,参数的类型会改变 在onSignIn函数中,我从firebase接收到一个值。

我正在尝试将firebase与react native(expo)集成。我遵循找到的firebase文档。文档非常直截了当,并按预期工作,但由于文档是JS格式的,并且我正在使用typescript进行编码,因此我的linter上出现了不少错误

关于下面的代码,我想谈几点

  • 我已经成功地定义了AuthenticatedUser,因为当我在signInWithGooglAsync中调用方法onSignIn(result)时,参数的类型会改变
  • 在onSignIn函数中,我从firebase接收到一个值。收到的值带有类型定义,但值。additionalUserInfo.profile被定义为对象| null |未定义,因此如果不将profile明确定义为any,我无法访问profile对象内的值。埃斯林特对此有点生气
  • 在isUserEqual函数中,googleUser对象没有名为getUserProfile()的方法。getUserId()。我试图显式地为它定义类型,但我不知道如何为有自己函数的函数(类?)定义类型
  • 我很确定我做了很多错事,并且在追逐一个兔子洞。因此,请有人建议我如何更好地实现这一点,或者告诉我如何解决第2点和第3点

    interface SuccessfullyAuthenticatedUser {
      ...
      type: "success";
      getBasicProfile?: any;
    }
    
    const signInWithGoogleAsync = async () => {
        try {
          const result = await Google.logInAsync({...});
    
          if (result.type === "success") {
            onSignIn(result); 
            ...
          }
          ...
        } catch (e) {
          ...
        }
      };
    
    
    const onSignIn = (googleUser: SuccessfullyAuthenticatedUser) => {
        ....
        ....
            firebase.auth().signInWithCredential(credential).then((value) => {
                const profile: any = value.additionalUserInfo?.profile;
                firebase.database().ref(...).set({
                  ...
                  first_name: profile.given_name,
                });
              })
          ...
      });
    };
    
    const isUserEqual = (
        googleUser: SuccessfullyAuthenticatedUser,
        firebaseUser: firebase.User | null,
      ) => {
        if (firebaseUser) {
          if (providerData[i]!.uid === googleUser.getBasicProfile().getId()){
                ...
          }
        return false;
      };