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

Allow null properties for Onyx.merge #350

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 2 additions & 10 deletions lib/Onyx.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import {Component} from 'react';
import {PartialDeep} from 'type-fest';
import * as Logger from './Logger';
import {
CollectionKey,
CollectionKeyBase,
DeepRecord,
KeyValueMapping,
OnyxCollection,
OnyxEntry,
OnyxKey,
} from './types';
import {CollectionKey, CollectionKeyBase, DeepRecord, KeyValueMapping, OnyxCollection, OnyxEntry, OnyxKey, NullableProperties} from './types';

/**
* Represents a mapping object where each `OnyxKey` maps to either a value of its corresponding type in `KeyValueMapping` or `null`.
Expand Down Expand Up @@ -210,7 +202,7 @@ declare function multiSet(data: Partial<NullableKeyValueMapping>): Promise<void>
* @param key ONYXKEYS key
* @param value Object or Array value to merge
*/
declare function merge<TKey extends OnyxKey>(key: TKey, value: PartialDeep<KeyValueMapping[TKey]>): Promise<void>;
declare function merge<TKey extends OnyxKey>(key: TKey, value: NullableProperties<PartialDeep<KeyValueMapping[TKey]>>): Promise<void>;

/**
* Clear out all the data in the store
Expand Down
17 changes: 13 additions & 4 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {IsEqual, Merge} from 'type-fest';
import {Merge} from 'type-fest';

/**
* Represents a deeply nested record. It maps keys to values,
Expand Down Expand Up @@ -106,9 +106,7 @@ type OnyxKey = Key | CollectionKey;
* The mapping is derived from the `values` property of the `TypeOptions` type.
*/
type KeyValueMapping = {
[TKey in keyof TypeOptions['values'] as TKey extends CollectionKeyBase
? `${TKey}${string}`
: TKey]: TypeOptions['values'][TKey];
[TKey in keyof TypeOptions['values'] as TKey extends CollectionKeyBase ? `${TKey}${string}` : TKey]: TypeOptions['values'][TKey];
};

/**
Expand Down Expand Up @@ -182,6 +180,16 @@ type OnyxEntry<TOnyxValue> = TOnyxValue | null;
*/
type OnyxCollection<TOnyxValue> = OnyxEntry<Record<string, TOnyxValue | null>>;

/**
* The `NullableProperties<T>` sets the values of all properties in `T` to be nullable (i.e., `| null`).
* It doesn't recurse into nested property values, this means it applies the nullability only to the top-level properties.
*
* @template T The type of the properties to convert to nullable properties.
*/
type NullableProperties<T> = {
[P in keyof T]: T[P] | null;
};

export {
CollectionKey,
CollectionKeyBase,
Expand All @@ -193,4 +201,5 @@ export {
OnyxEntry,
OnyxKey,
Selector,
NullableProperties,
};
Loading