Conversation
b95c8ec to
e389d32
Compare
|
@hayata-suenaga @blazejkustra I fixed some types based on your suggestions and improved other ones like |
This comment was marked as outdated.
This comment was marked as outdated.
… as string literals
neil-marcellini
left a comment
There was a problem hiding this comment.
I think it's good to go!
|
will review this tonight |
MemoWe can merge this PR now. There is a big change in Onyx recently, however, and the Onyx version in App is being bumped to reflect that change (this PR bumps the version). Let's wait until that PR is merged to merge the App setup PR that will bump the Onyx version further. |
Details
This PR creates type declarations for
Onyxfunctions andwithOnyxHOC.You can refer to this POC PR or checkout the POC branch to see more examples and test the typings.
Related Issues
#260
How it works
This library will use module augmentation in order to provide types from the consumer (the project who will use Onyx) to Onyx own functions.
To augment and add type-safety to Onyx functions and HOC, the developer needs to provides three types to Onyx:
keys,collectionKeys, andvalues.keys: An union type of all Onyx keys (excluding collection keys) used in the project.collectionKeys: An union type of all Onyx collection keys (excluding normal keys) used in the project. This distinction is necessary in order to provide better type-safety to Onyx.values: An object type which each key is a Onyx key, and each value is the data type that is going to be stored in that key. Both normal keys and collection keys has to be defined here.The developer will need to augment Onyx library by overriding those types inside
CustomTypeOptionsinterface.Example
This process is optional and if the developer choose to NOT augment it, the library will then work with minimal type-safety and support.
When augmenting the library, all our type declarations will use the types provided by the developer to add type-safety and support to all Onyx functions and HOC.
Type declarations
Onyx.getAllKeys()
Onyx.isSafeEvictionKey()
Onyx.removeFromEvictionBlockList()
Onyx.addToEvictionBlockList()
Onyx.connect()
Onyx.disconnect()
Onyx.set()
Onyx.multiSet()
Limitations
as constto the collection keys due to limitations in TS typing.Onyx.merge()
Onyx.clear()
Onyx.mergeCollection()
Limitations
as constto the keys due to limitations in TS typing.Onyx.update()
Limitations
as constto the collection keys inmergeCollectiondue to limitations in TS typing.Onyx.init()
Limitations
as constto the collection keys due to limitations in TS typing.Onyx.registerLogger()
withOnyx()
How it works
withOnyxHOC will accept two types,PropsandOnyxProps.OnyxPropsneed to be supplied to the HOC with its associated mapping object. The mapping object can have this properties:key: Can bestringorfunction. Specifies the Onyx key to supply the Onyx value to the prop. If passing afunction, the parameter of it will be thePropsof the component.canEvict: Can bebooleanorfunction. Specifies if the Onyx key can be evicted. If passing afunction, the parameter of it will be thePropsof the component.initWithStoredValues:boolean. If set to false, then no data will be prefilled into the component.selector:function. This will be used to subscribe to a subset of an Onyx key's data. The sourceData and withOnyx state are passed to the selector and should return the simplified data.PropsandOnyxPropsbecause all props specified inOnyxPropswill be injected in the component.OnyxPropsstripped because they were injected by the HOC and we don't want whoever uses it to need to pass those props to it.Limitations
Given all the possible combinations in the Mapping object of each Onyx prop, we have the following limitations:
stringkey:keymust match with the type ofonyxPropprop, otherwise an error will be thrown. ✅stringkey and selector:selectormust match with the type ofonyxPropprop, otherwise an error will be thrown. ✅functionkey:keyfunction must be a valid Onyx key and type of the Onyx value associated withkeymust match with the type ofonyxPropprop, otherwise an error will be thrown. ✅functionkey and selector:selectormust match with the type ofonyxPropprop, otherwise an error will be thrown. ✅stringcollection key:keymust match with the type ofonyxPropprop, otherwise an error will be thrown. ✅onyxProptype. ❌ WORKING ON THISstringcollection key and selector:selectormust match with the type ofonyxPropprop, otherwise an error will be thrown. ✅functioncollection key:keymust match with the type ofonyxPropprop, otherwise an error will be thrown. ✅functioncollection key and selector:selectormust match with the type ofonyxPropprop, otherwise an error will be thrown. ✅Overall, here are the limitations and issues with
withOnyxright now:selectoris losing partially its type-safety, to overcome this we can explicity specify its function signature to ensure type safety.ONYXKEYS.COLLECTION.REPORT, Onyx will return an object with all records e.g.{"report_0": {"id":"0", "value":"report_0"}, "report_1": {"id":"1", "value":"report_1"}}, translating into this TS typingRecord<string, Report | null>. At the moment we don't have means to differentiate betweenONYXKEYS.COLLECTION.REPORTand${ONYXKEYS.COLLECTION.REPORT}${reportId}which would only returnReport | null, causing mismatches between the Onyx value and the Onyx prop. I'm looking for a fix for that.Example