Rafael Cerávolo
Rafael Cerávolo

Reputation: 763

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

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?

Upvotes: 2

Views: 1558

Answers (3)

u4ii4y984484y203r
u4ii4y984484y203r

Reputation: 65

It took me ages to find the right solution for v9, hope it helps someone. You don't need the whole "firebase.firestore.FieldValue...", what works is actually much easier:

import { updateDoc, serverTimestamp } from "firebase/firestore";

const updateTimestamp = await updateDoc(docRef, {
    timestamp: serverTimestamp()
});

Upvotes: 0

user1689987
user1689987

Reputation: 1546

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

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317322

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

Upvotes: 8

Related Questions