Jakub Nam
Jakub Nam

Reputation: 11

Property 'x' does not exist on type ... - Promise syntax - TS2339

I met a problem I didn't understand with Typescript. I found another topics with that kind of problem but even that i dont know how to resolve my problem. I can console.log userCredential.user.metadata.b easily, but even that I got a error: Property 'b' does not exist on type 'UserMetadata'. Can u explain me what here is wrong?

Here the part of code

<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>

Thank you for help in advance

Upvotes: 1

Views: 971

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075567

TypeScript is inferring the type of userCredential based on the type information from the signInWithEmailAndPassword method. (Which is good, that's what you want.) The error is telling you that the type it's inferred for userCredential does have a user property with a metadata property, but the type of the metadata property doesn't have a property called b. The object has that property at runtime (since you can see it in console.log), but the type information for it doesn't say it has it.

To fix it, you'll need to change the type used by signInWithEmailAndPassword so that it gives metadata a b property (or an index signature, if that makes more sense in your use case).

Upvotes: 2

Related Questions