typescript - createUserWithEmailAndPassword and handling catch with firebase.auth.Error give compilation error TS2345 -


when call following method , want catch error , check error code can't specify type of error other error type can't access error. code firebase.auth.error.

methode description : (method) firebase.auth.auth.createuserwithemailandpassword(email: string, password: string): firebase.promise

specifing firebase.auth.auth in work firebase.auth.error give me compilation error.

error ts2345: argument of type '(error: error) => void' not assignable parameter of type '(a: error) => any'. types of parameters 'error' , 'a' incompatible. type 'error' not assignable type 'firebase.auth.error'. property 'code' missing in type 'error'. 

 

this.auth.createuserwithemailandpassword(username, password)                 .then( (auth: firebase.auth.auth) => { return auth; } )                 .catch( (error: firebase.auth.error) => {                      let errorcode = error.code;                     let errormessage = error.message;                      if (errormessage === "auth/weak-password") {                     alert("the password weak.");                     } else {                     alert(errormessage);                     }                     console.log(error);                  }); 

if in firebase.d.ts, see createuserwithemailandpassword has signature:

createuserwithemailandpassword(email: string, password: string): firebase.promise<any>; 

and firebase.promise extends firebase.promise_instance has signature catch:

catch(onreject?: (a: error) => any): firebase.thenable<any>; 

and that's why seeing error reported typescript: cannot pass arrow function receives firebase.auth.error, contains code property that's not present inerror.

you can cast received error firebase.auth.error can access code property without typescript error being effected:

this.auth.createuserwithemailandpassword(username, password)   .then((auth: firebase.auth.auth) => { return auth; } )   .catch((error: error) => {      let autherror = error firebase.auth.error;     let errorcode = autherror.code;     let errormessage = autherror.message;      if (errormessage === "auth/weak-password") {       alert("the password weak.");     } else {       alert(errormessage);     }     console.log(error);   }); 

also, don't need specify types parameters in arrow functions, typescript infer them. in fact, that's why error being effected in first place.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -