【问题标题】:Property does not exist on type 'function'类型“函数”上不存在属性
【发布时间】:2026-02-14 23:05:02
【问题描述】:

我正在尝试在使用 typescript 的函数中使用 firebase.auth 方法,但我得到的属性“名称”在“函数”类型上不存在(ts 2339)。我必须在这里遗漏一些简单的东西,但不太确定它是什么。

const registerUser = (email: String, password: String) => {
  console.log(email, password);
  return (firebase.auth as Function)
    .createUserWithEmailAndPassword(email, password)
    .then((userObj: Object) => console.log(email, password, userObj))
    .catch((error: Error) => console.log('Error logging in.', error));
};

问题发生在 .createUserWithEmailAndPassword 上

问题已解决:我试图通过将 firebase.auth 转换为 Function 类型来解决问题,通过删除它并通过 firebase.auth() 调用它很容易解决

【问题讨论】:

  • 为什么要将firebase.auth 转换为Function?这似乎是错误的。您的 TypeScript 应该与文档中的 JavaScript 相同:firebase.google.com/docs/auth/web/start#sign_up_new_users
  • 我把它改成 auth() 解决了这个问题,我想我可以通过将它转换为 Function 类型来解决它

标签: typescript firebase react-native firebase-authentication


【解决方案1】:

你的台词在这里:

return (firebase.auth as Function)

你得到的错误是Property does not exist on type 'function'

你确定firebase.auth 实际上是一个函数吗?如果它真的是一个函数,它应该类似于firebase.auth()

【讨论】:

    【解决方案2】:

    只需按照预期的方式调用 API,如 documentation 所示:

      return firebase.auth()
        .createUserWithEmailAndPassword(email, password)
        ...
    

    【讨论】: