【问题标题】:Angular firebase Parameter 'result' implicitly has an 'any' typeAngular firebase 参数“结果”隐含地具有“任何”类型
【发布时间】:2018-09-17 19:12:56
【问题描述】:

我有这段代码,结果和错误都有错误:

src/app/login/phone/phone.component.ts(48,75) 中的错误:错误 TS7006: 参数“结果”隐含地具有“任何”类型。 src/app/login/phone/phone.component.ts(53,14):错误 TS7006:参数 'error' 隐含了一个 'any' 类型。

 verifyLoginCode() {
        this.windowRef.confirmationResult.confirm(this.verificationCode).then(result => {

            this.user = result.user;

          })
          .catch(error => console.log(error, "Incorrect code entered?"));
      }

如何解决?

我正在使用 angularfire2、angular5。

【问题讨论】:

    标签: angular typescript firebase angularfire2


    【解决方案1】:

    这个错误的原因是 Angular 的 tsconfig.json 默认设置 noImplicitAny 标志为 true - "noImplicitAny": true,。有了这个,正在生成 Js 代码,但您也会收到错误,因为编译器无法推断类型。

    最简单的修复方法是then((result: any)

    现在您在评论中提到您尝试过then((result: String)。我敢打赌,您真正的意思是 then((result: string),因为 stringString 不一样string 是一个 Javascript 原语,使用文字创建 - ''"",而 String 是一个带有原型链的 Javascript object

    为了将来参考,您可以通过 console.log'inig 轻松检查类型(如果您无法通过其他方式知道):

    .then(result => {
      console.log(typeof result)
    })
    

    附言并且因为您使用this.user = result.user;,很明显result 不是一个字符串,而是Object 的一些ind。

    【讨论】:

      【解决方案2】:

      您的打字稿设置不允许您在不声明其类型的情况下指定变量。在本例中,then 方法接受参数“result”,但没有说明 TYPE“result”是什么。

      更新函数为

      .then((result: ResultType) => {
      

      其中ResultType 是任何结果的正确类型。

      此外,在catch 回调中,error 没有被赋予类型。您还需要将(error => 更改为((error: SomeType) =>

      听起来您实际上可能想禁用tsconfig compilerOptions 中的noImplicitAny 选项。您可能不想这样做的原因有很多(它迫使您更明确地了解您的类型,从长远来看这可能是件好事),但如果您只是在尝试或尝试学习,那么您可能不会想要选项集。

      如果此项目用于工作,或者由其他人管理(即您只是该项目的众多开发人员之一),那么您可能不想禁用它,因为他们可能希望 noImplicitAny 为他们自己的原因(代码质量)。

      【讨论】:

      • 无论我在那里写什么 --> resul: Resul 或 result: String ,错误都是一样的。请更具体。
      • 这完全是错误的。您必须指定类型的唯一原因是confirmany,因此回调参数result 没有类型信息。无论如何,这是一个不好的建议,并且会隐藏错误。相反,应更改 windowRef 的类型,以便 windowRef.confirmationResult.confirm 具有正确的类型。
      猜你喜欢
      • 2020-02-20
      • 2021-07-14
      • 1970-01-01
      • 2023-02-07
      • 2018-05-30
      • 2021-06-09
      • 2021-04-29
      • 2020-01-06
      • 1970-01-01
      相关资源
      最近更新 更多