将自己的firebase类从Javascript转换为Typescript会给我带来错误
我正在尝试将我以前的Javascript firebase类转换为Typescript,但我收到以下警告/错误:类型“firebase”上不存在属性“auth” 我试图寻找这个问题,但找不到明确的答案。有人知道我做错了什么吗将自己的firebase类从Javascript转换为Typescript会给我带来错误,typescript,firebase,firebase-authentication,Typescript,Firebase,Firebase Authentication,我正在尝试将我以前的Javascript firebase类转换为Typescript,但我收到以下警告/错误:类型“firebase”上不存在属性“auth” 我试图寻找这个问题,但找不到明确的答案。有人知道我做错了什么吗 class Firebase { constructor() { app.initializeApp(config) this.auth = app.auth() } doSignInWithGoogle = () => this.au
class Firebase {
constructor() {
app.initializeApp(config)
this.auth = app.auth()
}
doSignInWithGoogle = () => this.auth.signInWithPopup(this.googleProvider)
}
非常感谢您的帮助 将类中的auth属性声明为
私有auth代码>
您的代码应该如下所示:
class Firebase {
private auth;
constructor() {
app.initializeApp(config)
this.auth = app.auth()
}
doSignInWithGoogle = () => this.auth.signInWithPopup(this.googleProvider)
}
Firebase可以在Typescript中配置,如下所示。类属性app
和auth
可以是公共的,也可以是私有的,具体取决于您的预期设计
import * as firebase from 'firebase/app'
import 'firebase/auth'
class Firebase {
app: firebase.app.App
auth: firebase.auth.Auth
constructor() {
// https://firebase.google.com/docs/reference/js/firebase.app.App
this.app = firebase.initializeApp(config)
this.auth = firebase.auth(this.app)
}
public signInWithGooglePopup(): Promise<firebase.auth.UserCredential> {
// https://firebase.google.com/docs/reference/js/firebase.auth.Auth#sign-inwith-popup
return this.auth.signInWithPopup(this.googleProvider)
}
}
从“firebase/app”导入*作为firebase
导入“firebase/auth”
级火基{
应用程序:firebase.app.app
auth:firebase.auth.auth
构造函数(){
// https://firebase.google.com/docs/reference/js/firebase.app.App
this.app=firebase.initializeApp(配置)
this.auth=firebase.auth(this.app)
}
使用GooglePopup()的公共登录:承诺{
// https://firebase.google.com/docs/reference/js/firebase.auth.Auth#sign-带弹出窗口的输入
返回此.auth.signInWithPopup(此.googleProvider)
}
}
太好了,谢谢!但是我应该放什么类型的?私人身份验证:有吗?使用这个:firebase.auth.auth.@克里斯托弗·佩瑟特这很好用!非常感谢。这样注释以下内容是否也是一种好的做法:doSignInWithGoogle=():Record???@Kyle请参阅更新的示例,使用方法signInWithGooglePopup
。使用Typescript,您使用的类型越多,Typescript编译器就越有助于捕获错误。谢谢您提供的信息!我不熟悉typescript:)因为它实际上返回了一个承诺,我认为应该是这样的:dosinwinwithgoogle=():Promise@Kyle请参阅使用返回类型Promise
的方法更新。