You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Do you want to request a feature or report a bug? Feature
(If this is a usage question, please do not post it here—post it on gitter. If this is not a “feature” or a “bug”, or the phrase “How do I...?” applies, then it's probably a usage question.)
What is the current behavior?
No type info for the schema of used database.
What is the expected behavior?
Moar typez.
Basic idea
The library provides an empty interface:
exportinterfaceFirebaseRtdbSchema{}
Developer has his own types for the schema. Not related to the lib in any way, he can do whatever he wants.
Developer creates a .d.ts file where he extends the existing typings by merging in his own schema:
// make react-redux-firebase aware of the schema, globallyimport'react-redux-firebase'declare module 'react-redux-firebase'{exportinterfaceFirebaseRtdbSchemaextendsRtdbSchema{}}
Various methods and properties in the library can now use FirebaseRtdbSchema to bring more type safety by having a generic fnName<T extends Record<string, any> = FirebaseRtdbSchema>. If the developer doesn't provide anything, the globally declared schema is used; overriding mechanism allows for having multiple databases and such for advanced use cases.
Leveraging this means that the developer gets things like the following autocompletion.
Nested keys
When the lib requires specifying a path such as foo, foo.bar or foo/bar, we can provide a simple .join('/') function which has some magic types:
Or, we can build this idea into the existing functions which accept such path. This will allow proper type checking of nested keys.
interfaceSchema{one: stringtwo: {two_one: string}three: {three_one: {three_one_one: {three_one_one_one: stringthree_one_one_two: string}}}}chainOfKeys(['one'])chainOfKeys(['one','two'])// oopschainOfKeys(['two'])chainOfKeys(['two','two_one'])chainOfKeys(['three','three_one','three_one_one','tree_one_one_two'])// oops (can you see it?)
Query params
Similarly, we can bring more types to the current query thingy. Strings that are being parsed later don't play well with TypeScript: we need more semantics than that. We'd have to make some sort of query builder, like the following.
This would return some type such as Query<'score', 'likeThis'> which could be further used to check if score and likeThis are valid keys from the schema.
What about the rest?
I'm still exploring Firebase so some of these ideas might be naive because only work in very basic cases, so please tell me downsides and cases that this wouldn't cover.
Of course, this idea isn't only for Real Time Database (RTDB), but for all other similar things that Firebase has to offer.
The text was updated successfully, but these errors were encountered:
I really like this idea, thanks for posting! Got a laugh out of some of the wording like "more types to the current query thingy" 😆 I am a really big fan of the fact that doing the typing of all of this stuff makes the lack of naming of certain things so apparent.
prescottprue
changed the title
Providing type for the schema
feat(core): support providing type for firebase state
Jul 13, 2019
prescottprue
changed the title
feat(core): support providing type for firebase state
feat(types): support providing type for firebase state
Oct 11, 2019
Do you want to request a feature or report a bug? Feature
(If this is a usage question, please do not post it here—post it on gitter. If this is not a “feature” or a “bug”, or the phrase “How do I...?” applies, then it's probably a usage question.)
What is the current behavior?
No type info for the schema of used database.
What is the expected behavior?
Moar typez.
Basic idea
The library provides an empty interface:
Developer has his own types for the schema. Not related to the lib in any way, he can do whatever he wants.
Developer creates a
.d.ts
file where he extends the existing typings by merging in his own schema:Various methods and properties in the library can now use
FirebaseRtdbSchema
to bring more type safety by having a genericfnName<T extends Record<string, any> = FirebaseRtdbSchema>
. If the developer doesn't provide anything, the globally declared schema is used; overriding mechanism allows for having multiple databases and such for advanced use cases.Leveraging this means that the developer gets things like the following autocompletion.
Nested keys
When the lib requires specifying a path such as
foo
,foo.bar
orfoo/bar
, we can provide a simple.join('/')
function which has some magic types:Or, we can build this idea into the existing functions which accept such path. This will allow proper type checking of nested keys.
Query params
Similarly, we can bring more types to the current query thingy. Strings that are being parsed later don't play well with TypeScript: we need more semantics than that. We'd have to make some sort of query builder, like the following.
This would return some type such as
Query<'score', 'likeThis'>
which could be further used to check ifscore
andlikeThis
are valid keys from the schema.What about the rest?
I'm still exploring Firebase so some of these ideas might be naive because only work in very basic cases, so please tell me downsides and cases that this wouldn't cover.
Of course, this idea isn't only for Real Time Database (RTDB), but for all other similar things that Firebase has to offer.
The text was updated successfully, but these errors were encountered: