How do you access the "code" property of a FirebaseError in AngularFire / TypeScript?

angular, angularfire, firebase, typescript

Solution

Another resolution, you can try import global FirebaseError from '@firebase/util' package and check using type guard as below.

import { FirebaseError } from '@firebase/util'

try {
    // Some firebase functions
    await signInWithEmailAndPassword(auth, email, password)
} catch (error: unknown) {
   if (error instanceof FirebaseError) {
      console.error(error.code)
   }
}

Problem

FirebaseError has a "code" property, but how do you read it in the catch method of a promise? The following throws a TypeScript error of: `Property 'code' does not exist on type 'Error'.` ``` this.af.database .object(`/some/path`) .set(newObj) .then(data => { console.log('success'); }) .catch(err => { // Property 'code' does not exist on type 'Error'. console.log(`code`, err.code); }); ```

Original source