question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Defining TypeScript interface types for documents of a Snapshots

See original GitHub issue

Wouldn’t it be somehow possible to define the types of the documents which are returned by snapshot.data() and snapshot.get() even better with TypeScript?

Currently, when calling these methods on a snapshot, the type is unknown… I think this could be even better if we as developers could define and describe the documents which are fetched into snapshots. I think that maybe one could use generics where an interface type is put in like this:

const userSnap: FirebaseFirestore.DocumentSnapshot<UserDoc> = await firestore.collection("users").doc(uid).get();
const languageRef: FirebaseFirestore.DocumentReference = userSnap.get("language");

Here I define in a generic the type that the object should have when I call snapshot.data():

const userSnap: FirebaseFirestore.DocumentSnapshot<UserDoc>

I define a type UserDoc which I can then define as a generic of DocumentSnapshot and with this definition the call to .data() returns an object with the correct type of UserDoc AND even better would be if even the call to snapshot.get() would return the correct type here:

const languageRef: FirebaseFirestore.DocumentReference = userSnap.get("language");

So that I don’t explicitly have to define the type of languageRef and TypeScript knows exactly that the type should be of FirebaseFirestore.DocumentReference, without any further type definition.

So this code:

const userSnap: FirebaseFirestore.DocumentSnapshot = await firestore.collection("users").doc(uid).get();
const languageRef: FirebaseFirestore.DocumentReference = userSnap.get("language");

becomes this:

const userSnap: FirebaseFirestore.DocumentSnapshot<UserDoc> = await firestore.collection("users").doc(uid).get();
const languageRef = userSnap.get("language");

In my opinion a lot more readable and with a good typescript compatilbe editor at least as good as defining the type manually each time you call .get() or .data()

I love to hear what you think, if this is possible or something as useful for others as it would be for me.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:14
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
schmidt-sebastiancommented, Mar 2, 2021

@creativecreatorormaybenot Yes, this is implemented (as shown by your code snippets).

1reaction
creativecreatorormaybenotcommented, Mar 1, 2021

@IchordeDionysos That is pretty sweet, thanks 😋

For reference:

const userConverter = {
  toFirestore(user: User): firebase.firestore.DocumentData {
    return {name: user.name, id: user.id};
  },
  fromFirestore(
    snapshot: firebase.firestore.QueryDocumentSnapshot,
    options: firebase.firestore.SnapshotOptions
  ): User {
    const data = snapshot.data(options)!;
    return new User(data.name, data.id);
  }
}

Or for interfaces (akin to my previous comment):

const postConverter = {
  toFirestore(user: User): firebase.firestore.DocumentData {
    return user;
  },
  fromFirestore(
    snapshot: firebase.firestore.QueryDocumentSnapshot,
    options: firebase.firestore.SnapshotOptions
  ): User {
    return snapshot.data(options) as User;
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Handbook - Interfaces - TypeScript
In TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well...
Read more >
(Typescript) Cleanest way to provide my own typings for ...
1 Answer 1 ... If you want to assume that the entire snapshot is an object structured like Doc , this is very...
Read more >
QuerySnapshot | JavaScript SDK | Node.js (client) API reference
Returns an array of the documents changes since the last snapshot. If this is the first snapshot, all documents will be in the...
Read more >
Using Firestore With Typescript - Medium
I have to import the type in each file I use it. What if I get the type wrong? More code to write...
Read more >
TypeScript and MST - MobX-state-tree
When using models, you write an interface, along with its property types, that will be used to perform type checks at runtime. What...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found