-
Notifications
You must be signed in to change notification settings - Fork 894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add type and default generic type to DataSnapshot #590
Conversation
- allow AngularFire2 list snapshotchanges to have generic type support
Hello, is that any problem with that changes? I think it would be nice for angularfire2. Hope it will be merge soon |
The RTDB SDK for Android handles this situation a little differently. We have getValue() overloads that allow you to specify the return type (https://firebase.google.com/docs/reference/android/com/google/firebase/database/DataSnapshot.html). This nicely works around the problem of adding type information to DataSnapshot itself, which should theoretically also result in changes to all API methods that return DataSnapshots(*). @iNViTiON, @hiepxanh: Could we get away with something simply as:
If that doesn't help you, then I am fine to merge this change as is. (*) Please don't do this, as this doesn't quite work for a schema-less database. |
@schmidt-sebastian that will cause something like this to happen.
|
@hiepxanh that commit only for valueChanges but not affect snapshotChanges. My PR rely a lot on his PR. 😅 |
Please make the same change in firebase-js-sdk/packages/firebase/index.d.ts Line 427 in b351749
This change here is fine if it addresses a need for AngularFire2. In general, adding typings to DataSnapshot enforces a schema when we we don't really have one (note that your Please note that there are other places in the API where it might make sense to add generics to DataSnapshot (such as |
yahoo! thank you for willing accept to that PR. 😍 |
@schmidt-sebastian pushed and waiting for CI result. I also added nullable to val() in this commit since it will return null when the path was empty. Is this a right thing to do? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍 leave it up to you @schmidt-sebastian
How does this change help anything? const ref = db.collection('foo').doc('bar');
const snap = await ref.get(); In this snippet const snap = await ref.get() as DocumentSnapshot<T>;
const val = snap.val(); better than const snap = await ref.get();
const val = snap.val() as T; ? |
FYI I've done some interface work for AngularFire in angular/angularfire#1644 and angular/angularfire#1643; seems like there's some duplicate work going on here. I'm happy with my changes which extend DataSnapshot, DocumentSnapshot, and QueryDocumentSnapshot living in AngularFire but maybe we want to consider bringing it in here? Also making a union of exist vs. not cases allows for the use of type guards so one doesn't have to use bangs on val, which is nice. |
@iNViTiON Sorry to keep this unmerged for so long. We would like to address how to handle the typings for AngularFire for both the Realtime Database and Firestore before we go ahead and merge this. I will update you (and hopefully merge this PR) by end of week. |
Thanks, @schmidt-sebastian. @jamesdaniels maybe we should wait until @firebase team settled this. So @AngularFire2 code can fully support it in the same direction. |
@iNViTiON I think @davideast and I are comfortable having extended types in AngularFire for now; we already have quite a few. In fact, we just merged angular/angularfire#1643, it will be included in the next @schmidt-sebastian I'm WFH and trying my best to avoid meetings today, but let's find some time this week to discuss. |
@jamesdaniels yes, I notice you're in Firebase team but I'm not sure how the team manage this. That a good news. 😊 |
Quick update: We discussed this PR today and are investigating if we can push this change down to AngularFire in its entirety. |
As per @jamesdaniels, this issue is now fulled addressed in AngularFire2 (per angular/angularfire#1643). We might revisit pushing some of this typing down to the main JS client, but will hold off on it for now. |
Allow AngularFire2 list snapshotchanges to have generic type support.
This not affect any usage since it included default type.