Firebase v8 Typescript definition import for User

Viewed 6024

With previous version of Firebase I use to import the Typescript definition of the User as following:

import {User} from 'firebase';

Following the introduction of v8 this import does not work anymore:

Module '"../../../../node_modules/firebase"' has no exported member 'User'. Did you mean to use 'import User from "../../../../node_modules/firebase"' instead?

The release notes points the fact that the CJS bundles was dropped, nevertheless, does not mention how this import should now be resolved.

Any help appreciated, thank you in advance.

5 Answers

You can say this instead:

import firebase from "firebase/app"
const user: firebase.User = ...

Or if you want to abbreviate it:

import firebase from "firebase/app"
type User = firebase.User
const user: User = ...

For V9.0.0(Web version 9 (modular), this can be done by

// make alias for greater readability
import { User as FirebaseUser } from "firebase/auth";

Then you can use it by:

// here direct use the type inside a hooks 
const [user, setUser] = useState<FirebaseUser | null>(null)

I was able to solve my issue while importing the User with thee Firebase JS SDK v8.0.0 as following:

import {User} from '@firebase/auth-types';

Have you tried importing firebase and then accessing User with firebase.User ? It's obviously way more chunky but worked for me.

Edit: I was also having issues with conflicting imports previously - where I had a file called firebase.ts that was causing my app to zoink out. Just mentioning it in case you by chance have something similar

another alternative to importing firebase is to add the firebase types in an index.d.ts file:

/// <reference types="firebase" />

type Firebase = typeof firebase.default

type FirebaseApp = firebase.default.app.App

type FirebaseUser = firebase.default.User

Note: I had to do firebase.default instead of firebase because I am using triple slash directives /// <reference types="..." />

You can omit .default by just importing firebase in the index.d.ts file:

import firebase from "firebase/app"

type Firebase = typeof firebase

type FirebaseApp = firebase.app.App

type FirebaseUser = firebase.User
Related