【问题标题】:Property 'x' does not exist on type ... - Promise syntax - TS2339类型上不存在属性“x”... - Promise 语法 - TS2339
【发布时间】:2020-06-29 18:26:10
【问题描述】:

我遇到了一个我不理解 Typescript 的问题。我发现了另一个有这种问题的主题,但即使我不知道如何解决我的问题。 我可以很容易地 console.log userCredential.user.metadata.b,但即使我得到了一个错误:属性'b'在类型'UserMetadata'上不存在。你能解释一下这里有什么问题吗?

这里是代码部分

<pre>
 login(email: string, password: string): Promise<any> {
       return this.afAuth.signInWithEmailAndPassword(email, password)
           .then(userCredential => {
               const creationTime: number = userCredential.user.metadata.b; // <-here is a problem
               console.log('creationTinme', creationTime);
               this.handleAuthentication(
                   userCredential.user.email,
                   userCredential.user.uid,
                   'xxx',
                   creationTime
                   // metadata.creationTime expiresInuser.metadata
               );
           })
           .catch(error => {
                console.log(error);
           });
</pre>

提前感谢您的帮助

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    TypeScript 正在根据来自 signInWithEmailAndPassword 方法的类型信息推断 userCredential 的类型。 (这很好,这就是你想要的。)错误告诉你它为userCredential 推断的类型确实有一个user 属性和metadata 属性,但metadata 属性的类型没有' t 有一个名为b 的属性。该对象在运行时具有该属性(因为您可以在 console.log 中看到它),但它的类型信息并没有说明它具有它。

    要修复它,您需要更改signInWithEmailAndPassword 使用的类型,以便它为metadata 提供b 属性(或index signature,如果这在您的用例中更有意义)。

    【讨论】:

    • 我是初学者,我现在不明白这个决心,但我会阅读并了解它,然后我会回答,谢谢
    猜你喜欢
    • 2019-01-11
    • 2019-04-11
    • 2020-12-01
    • 2016-11-14
    • 2021-08-10
    • 2016-03-14
    相关资源
    最近更新 更多