1

I am using Typescript and NodeJS for my Google Cloud Function

if I get a document from firestore like this

const result = await db.doc(`events/${eventID}`).get();
const myData = result.data();

then the type of myData is FirebaseFirestore.DocumentData like this enter image description here

in other part of my code,

I need to check, if a variable has the type of FirebaseFirestore.DocumentData or not using the code below

if ( myVariable instanceof FirebaseFirestore.DocumentData ) {

   // do something

}

but I have an error like this

enter image description here

Property 'DocumentData' does not exist on type 'typeof FirebaseFirestore'

so how to check if my variable is a type of Firestore Document Data?

I see this answer , and it needs to import something so I can access it. how to do something like that for FirebaseFirestore.DocumentData ?

sarah
  • 3,819
  • 4
  • 38
  • 80
  • Not directly an answer but couldn't you simply check whether result.data() is not undefined as the only options for it are to be undefined or of type document data. – Maurice Mar 21 '21 at 10:09
  • I am sorry, it is my bad. I will revise my question. the code above is actually a simplified version of my real case. when I need to check if my variable is FirebaseFirestore.DocumentData or not, it is no longer optional anymore, I mean, actually I need to check if my variable is a FirebaseFirestore.DocumentData or another complicated data type (not undefined) – sarah Mar 21 '21 at 10:13

1 Answers1

0

As with your other question you are mixing up Typescript types and JavaScript objects.

FirebaseFirestore.DocumentData refers to a type that is used by the TypeScript compiler as a compile time check. TypeScript compiles down to JavaScript and JavaScript doesn't know about those types during runtime. So the only way to check if the returned data is the Document Data you have to check for the existence of fields or check for undefined.

Johannes Klauß
  • 10,676
  • 16
  • 68
  • 122