How to proper import Timestamp class from firebase (8+) using Typescript

Viewed 780

I was using firebase 7.16.1 and I was importing and declaring a property as Timestamp this way:

import { firestore } from 'firebase/app';

export class CourseEventMessage {
  sentTimestamp: firestore.Timestamp;
}

But after upgrading to firebase 8.1.2, the import is getting an error:

Module '"../../../node_modules/firebase"' has no exported member 'firestore'. Did you mean to use 'import firestore from "../../../node_modules/firebase"' instead?ts(2614)

I've tried the following imports, all of them imports ok, but no Timestamp exists on the import.

import firestore from "../../../node_modules/firebase"; 
import firestore from 'firebase';
import firestore from 'firebase/app';

The only way I could found that imports Timestamp was:

import * as firebase from 'firebase/app';

export class CourseEventMessage {
    sentTimestamp: firebase.default.firestore.Timestamp;
}

So what's the proper way to import the Timestamp class?

2 Answers

In v8, all of the types for all Firebase products should come from "firebase/app". It's typical to start an import like this, as you see in the documentation for module bundlers:

import firebase from "firebase/app"

Do NOT use the old form from v7:

import * as firebase from "firebase/app"  // this no longer works the way you expect

Timestamp can be found in firebase.firestore.Timestamp.

import firebase from 'firebase/app';

export class CourseEventMessage {
    sentTimestamp: firebase.firestore.Timestamp;
}

If you want to abbreviate that a bit, use a type alias:

type Timestamp = firebase.firestore.Timestamp;
export class CourseEventMessage {
    sentTimestamp: Timestamp;
}

For v9 its: import { Timestamp } from 'firebase/firestore';

Related