-
Notifications
You must be signed in to change notification settings - Fork 74
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 declarations #267
Add type declarations #267
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.
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.
Looking good
… as string literals
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.
👍
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! 🥇
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.
I think it's good to go!
will review this tonight |
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.
🥳 🥳 🥳 🥳 🥳
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
Onyx
functions andwithOnyx
HOC.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
CustomTypeOptions
interface.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 const
to the collection keys due to limitations in TS typing.Onyx.merge()
Onyx.clear()
Onyx.mergeCollection()
Limitations
as const
to the keys due to limitations in TS typing.Onyx.update()
Limitations
as const
to the collection keys inmergeCollection
due to limitations in TS typing.Onyx.init()
Limitations
as const
to the collection keys due to limitations in TS typing.Onyx.registerLogger()
withOnyx()
How it works
withOnyx
HOC will accept two types,Props
andOnyxProps
.OnyxProps
need to be supplied to the HOC with its associated mapping object. The mapping object can have this properties:key
: Can bestring
orfunction
. Specifies the Onyx key to supply the Onyx value to the prop. If passing afunction
, the parameter of it will be theProps
of the component.canEvict
: Can beboolean
orfunction
. Specifies if the Onyx key can be evicted. If passing afunction
, the parameter of it will be theProps
of 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.Props
andOnyxProps
because all props specified inOnyxProps
will be injected in the component.OnyxProps
stripped 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:
string
key:key
must match with the type ofonyxProp
prop, otherwise an error will be thrown. ✅string
key and selector:selector
must match with the type ofonyxProp
prop, otherwise an error will be thrown. ✅function
key:key
function must be a valid Onyx key and type of the Onyx value associated withkey
must match with the type ofonyxProp
prop, otherwise an error will be thrown. ✅function
key and selector:selector
must match with the type ofonyxProp
prop, otherwise an error will be thrown. ✅string
collection key:key
must match with the type ofonyxProp
prop, otherwise an error will be thrown. ✅onyxProp
type. ❌ WORKING ON THISstring
collection key and selector:selector
must match with the type ofonyxProp
prop, otherwise an error will be thrown. ✅function
collection key:key
must match with the type ofonyxProp
prop, otherwise an error will be thrown. ✅function
collection key and selector:selector
must match with the type ofonyxProp
prop, otherwise an error will be thrown. ✅Overall, here are the limitations and issues with
withOnyx
right now:selector
is 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.REPORT
and${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