mirror of
https://github.com/appy-one/acebase-core.git
synced 2026-05-24 22:01:23 -06:00
[GH-ISSUE #20] Allow better types for DataReference and DataSnapshot #15
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: github-starred/acebase-core#15
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @futurGH on GitHub (Oct 3, 2022).
Original GitHub issue: https://github.com/appy-one/acebase-core/issues/20
DataSnapshot#val()always returnsany, despite the fact that, given the existence ofTypeMappingsbinding, a user will often know at compile time what type will be returned. I have a few suggestions for type-level changes with no runtime impact, apart from the 3rd option, listed roughly in order from least change to acebase-core to greatest internal change. I'd be happy to submit a PR for any of them, just wanted to get feedback first.Option 1 (Generic-as-cast)
Workbench Example
snapshot.val() as MyClassOption 2 (User declaration merging)
Workbench Example
TypeMappings#bindanyfor users not using type mappingsOption 3 (
bindas assertion function)Workbench Example
TypeMappingsbindto be implemented as a top level method ofAceBase, as current TypeScript limitations prevent the functionality used from working if it's nested in a member class@appy-one commented on GitHub (Oct 4, 2022):
Thanks, that all looks great! I'll try out the code from the workbenches, great stuff!
@appy-one commented on GitHub (Oct 17, 2022):
Sorry it's taking a while to get back to you, I've been working to port all last js bits of all acebase packages to typescript. I'm almost finished, so I'll take another look at this once everything is working well and published. Thanks again!
@futurGH commented on GitHub (Oct 17, 2022):
No worries! Let me know what you think whenever you can find the time to take a look and I'd be happy to get to work on an implementation.
@Bubz43 commented on GitHub (Oct 19, 2022):
I've personally been using something similar to option 2 and it seems to work quite well.
Minimal Example Playground
Throw any object interface at it and you are good to go, though you are lying about the shape of the DB data and have to keep in mind you need some other way actually guarantee correctness on initial creation/migrations.
@futurGH commented on GitHub (Oct 19, 2022):
Totally agree, thanks for the example! With compile-time–only type checking like TypeScript's, there's always the risk that data won't line up with expectations, especially when it's being moved across file system boundaries as in the case of a DB. I've been using a version of option 2 in my own projects as well, and it's definitely best accompanied by a runtime function to assert that the received type is indeed what you're expecting.
@futurGH commented on GitHub (Feb 3, 2023):
Hope you don't mind a quick bump @appy-one — I haven't been using AceBase as of late so if there's interest I'd love to submit a PR while the implementation is somewhere in my brain 🙂
@appy-one commented on GitHub (Feb 7, 2023):
@futurGH yes please, you are more than welcome to! I think option 1 would be the best starting point for now, maybe option 3 at a later stage!