Skip to content
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

Merged
merged 44 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
7d8ed5c
Generate `Onyx` and `withOnyx` declaration files
fabioh8010 Jun 27, 2023
d10db07
Create `Logger` declaration file
fabioh8010 Jun 27, 2023
98ac024
Improve basic `Onyx` method declarations
fabioh8010 Jun 27, 2023
e0b50fd
Ignore .d.ts files during linting
fabioh8010 Jun 27, 2023
65f0cf3
Add "types" entry in package.json
fabioh8010 Jun 28, 2023
2eb85e6
Initial implementation of generic keys
fabioh8010 Jun 28, 2023
64a1d1a
Improve typings for Onyx methods
fabioh8010 Jun 29, 2023
dbfbd8c
Improve custom type approach
fabioh8010 Jun 30, 2023
78a766c
Address reviews suggestions and improve several types
fabioh8010 Jun 30, 2023
e389d32
Minor fixes
fabioh8010 Jul 3, 2023
cd1807b
Improve merge() and updated() typings
fabioh8010 Jul 4, 2023
533dfdf
Improves mergeCollection() typings
fabioh8010 Jul 4, 2023
5cce58e
Add type to withOnyxInstance property on connect()
fabioh8010 Jul 4, 2023
d42ea7e
Change callback type based on waitForCollectionCallback on connect()
fabioh8010 Jul 4, 2023
0b487d5
Improve connect() typing
fabioh8010 Jul 4, 2023
b0a0321
First version of withOnyx HOC typing
fabioh8010 Jul 10, 2023
c5b36a8
Fix withOnyx key type and improve all typings
fabioh8010 Jul 12, 2023
af68220
Use a separate type for collection keys
fabioh8010 Jul 13, 2023
7b3492a
Fix update() mergeCollection object
fabioh8010 Jul 13, 2023
7944e13
Improve connect() type for collection keys
fabioh8010 Jul 13, 2023
2592b36
Fix Selector type for unknown values
fabioh8010 Jul 13, 2023
02f0c1e
General improvements on HOC typings
fabioh8010 Jul 17, 2023
a25920a
Minor fix in getAllKeys() type
fabioh8010 Jul 18, 2023
e0ebe55
Address review comments
fabioh8010 Jul 19, 2023
6103f73
Address review comments
fabioh8010 Jul 25, 2023
32abd45
Remove string selectors and fix safeEvictionKeys
fabioh8010 Jul 26, 2023
6ec2d95
First attempt of GetOnyxValue
fabioh8010 Jul 26, 2023
38e8eed
Merge branch 'main' into feature/type-declarations
fabioh8010 Jul 26, 2023
30b2dc4
Add type declarations for new Onyx methods
fabioh8010 Jul 26, 2023
4332d2f
Add comments to some draft types
fabioh8010 Jul 26, 2023
343d7d9
Second attempt to solve collection key issue
fabioh8010 Jul 27, 2023
949b48e
Add JSDoc comments for several types
fabioh8010 Jul 27, 2023
3a101df
Clean up typings and add more JSDoc comments
fabioh8010 Jul 28, 2023
f06ce38
Merge remote-tracking branch 'origin/main' into feature/type-declarat…
fabioh8010 Jul 31, 2023
826ee48
Address review comments
fabioh8010 Jul 31, 2023
834bf1d
Address review comments
fabioh8010 Aug 1, 2023
3eaf539
Replace MergeBy with Merge from type-fest and remove unused exports
fabioh8010 Aug 2, 2023
6215c2f
Add examples for OnyxEntry and OnyxCollectionEntries types
fabioh8010 Aug 2, 2023
c5fc96e
Change KeyValueMapping type to automatically consider collection keys…
fabioh8010 Aug 2, 2023
521426f
Address review comments
fabioh8010 Aug 3, 2023
d632845
First test with new OnyxUpdate approach
fabioh8010 Aug 4, 2023
eae275b
Remove unused code
fabioh8010 Aug 4, 2023
86aa549
Export OnyxUpdate type
fabioh8010 Aug 4, 2023
500d228
Address review comments
fabioh8010 Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions lib/Onyx.d.ts
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import {
CollectionKey,
CollectionKeyBase,
DeepRecord,
Key,
KeyValueMapping,
OnyxCollectionEntries,
OnyxKey,
OnyxCollection,
OnyxEntry,
OnyxKey,
} from './types';

fabioh8010 marked this conversation as resolved.
Show resolved Hide resolved
/**
Expand All @@ -35,15 +34,15 @@ type BaseConnectOptions<TKey extends OnyxKey> = {
* The type is built from `BaseConnectOptions` and extended to handle key/callback related options.
* It includes two different forms, depending on whether we are waiting for a collection callback or not.
*
* If `waitForCollectionCallback` is `true`, it expects `key` to be a Onyx collection key and `callback` will pass `value` as an `OnyxCollectionEntries`.
* If `waitForCollectionCallback` is `true`, it expects `key` to be a Onyx collection key and `callback` will pass `value` as an `OnyxCollection`.
*
* If `waitForCollectionCallback` is `false` or not specified, the `key` can be any Onyx key and `callback` will pass `value` as an `OnyxEntry`.
fabioh8010 marked this conversation as resolved.
Show resolved Hide resolved
*/
type ConnectOptions<TKey extends OnyxKey> = BaseConnectOptions<TKey> &
(
| {
key: TKey extends CollectionKey ? TKey : never;
callback?: (value: OnyxCollectionEntries<KeyValueMapping[TKey]>, key?: TKey) => void;
callback?: (value: OnyxCollection<KeyValueMapping[TKey]>, key?: TKey) => void;
waitForCollectionCallback: true;
fabioh8010 marked this conversation as resolved.
Show resolved Hide resolved
}
| {
Expand Down
4 changes: 2 additions & 2 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Onyx from './Onyx';
import {CustomTypeOptions, OnyxCollectionEntries, OnyxEntry} from './types';
import {CustomTypeOptions, OnyxCollection, OnyxEntry} from './types';
import withOnyx from './withOnyx';

export default Onyx;
export {CustomTypeOptions, OnyxCollectionEntries, OnyxEntry, withOnyx};
export {CustomTypeOptions, OnyxCollection, OnyxEntry, withOnyx};
85 changes: 69 additions & 16 deletions lib/types.d.ts
neil-marcellini marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {IsEqual} from 'type-fest';

/**
* Represents a merged object between `TObject` and `TOtherObject`.
* If both objects have a property with the same name, the type from `TOtherObject` is used.
*/
type MergeBy<TObject, TOtherObject> = Omit<TObject, keyof TOtherObject> & TOtherObject;
import {IsEqual, Merge} from 'type-fest';
neil-marcellini marked this conversation as resolved.
Show resolved Hide resolved

/**
* Represents a deeply nested record. It maps keys to values,
Expand All @@ -24,7 +18,7 @@ type DeepRecord<TKey extends string | number | symbol, TValue> = {[key: string]:
* The user-defined options (CustomTypeOptions) are merged into these predefined options.
* In case of conflicting properties, the ones from CustomTypeOptions are prioritized.
*/
type TypeOptions = MergeBy<
type TypeOptions = Merge<
{
keys: string;
collectionKeys: string;
Expand All @@ -42,6 +36,7 @@ type TypeOptions = MergeBy<
* ```ts
* // ONYXKEYS.ts
* import {ValueOf} from 'type-fest';
* import { Account, Report } from './types';
*
* const ONYXKEYS = {
* ACCOUNT: 'account',
Expand All @@ -60,7 +55,7 @@ type TypeOptions = MergeBy<
* type OnyxValues = {
* [ONYXKEYS.ACCOUNT]: Account;
* [ONYXKEYS.IS_SIDEBAR_LOADED]: boolean;
* [report: `${typeof ONYXKEYS.COLLECTION.REPORT}${string}`]: Report;
* [ONYXKEYS.COLLECTION.REPORT]: Report;
* };
*
* export default ONYXKEYS;
Expand Down Expand Up @@ -102,9 +97,19 @@ type CollectionKey = `${CollectionKeyBase}${string}`;
type OnyxKey = Key | CollectionKey;

/**
* Represents a Record where each key is an Onyx key and each value is its corresponding Onyx value type.
* Represents a mapping of Onyx keys to values, where keys are either normal or collection Onyx keys
* and values are the corresponding values in Onyx's state.
*
* For collection keys, `KeyValueMapping` allows any string to be appended
* to the key (e.g., 'report_some-id', 'download_some-id').
*
* The mapping is derived from the `values` property of the `TypeOptions` type.
*/
type KeyValueMapping = TypeOptions['values'];
type KeyValueMapping = {
[TKey in keyof TypeOptions['values'] as TKey extends CollectionKeyBase
? `${TKey}${string}`
: TKey]: TypeOptions['values'][TKey];
};

/**
* Represents a selector function type which operates based on the provided `TKey` and `ReturnType`.
Expand All @@ -127,13 +132,63 @@ type Selector<TKey extends OnyxKey, TReturnType> = IsEqual<KeyValueMapping[TKey]

/**
* Represents a single Onyx entry, that can be either `TOnyxValue` or `null` if it doesn't exist.
*
* It can be used to specify data retrieved from Onyx e.g. `withOnyx` HOC mappings.
*
* @example
* ```ts
* import Onyx, {OnyxEntry, withOnyx} from 'react-native-onyx';
*
* type OnyxProps = {
* userAccount: OnyxEntry<Account>;
* };
*
* type Props = OnyxProps & {
* prop1: string;
* };
*
* function Component({prop1, userAccount}: Props) {
* // ...
* }
*
* export default withOnyx<Props, OnyxProps>({
* userAccount: {
* key: ONYXKEYS.ACCOUNT,
* },
* })(Component);
* ```
*/
fabioh8010 marked this conversation as resolved.
Show resolved Hide resolved
type OnyxEntry<TOnyxValue> = TOnyxValue | null;

/**
* Represents an Onyx collection of entries, that can be either a record of `TOnyxValue`s or `null` if it is empty or doesn't exist.
*
* It can be used to specify collection data retrieved from Onyx e.g. `withOnyx` HOC mappings.
*
* @example
* ```ts
* import Onyx, {OnyxCollection, withOnyx} from 'react-native-onyx';
*
* type OnyxProps = {
* reports: OnyxCollection<Report>;
* };
*
* type Props = OnyxProps & {
* prop1: string;
* };
*
* function Component({prop1, reports}: Props) {
* // ...
* }
*
* export default withOnyx<Props, OnyxProps>({
* reports: {
* key: ONYXKEYS.COLLECTION.REPORT,
* },
* })(Component);
* ```
*/
type OnyxCollectionEntries<TOnyxValue> = OnyxEntry<Record<string, TOnyxValue | null>>;
type OnyxCollection<TOnyxValue> = OnyxEntry<Record<string, TOnyxValue | null>>;

export {
CollectionKey,
Expand All @@ -142,10 +197,8 @@ export {
DeepRecord,
Key,
KeyValueMapping,
MergeBy,
OnyxCollection,
OnyxEntry,
OnyxKey,
Selector,
TypeOptions,
OnyxEntry,
OnyxCollectionEntries,
};
10 changes: 2 additions & 8 deletions lib/withOnyx.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {IsEqual} from 'type-fest';
import {CollectionKeyBase, KeyValueMapping, OnyxCollectionEntries, OnyxEntry, OnyxKey, Selector} from './types';
import {CollectionKeyBase, KeyValueMapping, OnyxCollection, OnyxEntry, OnyxKey, Selector} from './types';

/**
* Represents the base mapping options between an Onyx key and the component's prop.
Expand Down Expand Up @@ -116,13 +116,7 @@ type CollectionMapping<
TOnyxKey extends CollectionKeyBase
> = BaseMapping<TComponentProps, TOnyxProps> &
(
| BaseMappingKey<
TComponentProps,
TOnyxProps,
TOnyxProp,
TOnyxKey,
OnyxCollectionEntries<KeyValueMapping[TOnyxKey]>
>
| BaseMappingKey<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey, OnyxCollection<KeyValueMapping[TOnyxKey]>>
| BaseMappingStringKeyAndSelector<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey>
| BaseMappingFunctionKeyAndSelector<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey>
);
Expand Down