Skip to content

Commit

Permalink
Merge pull request #350 from blazejkustra/fix/typescript-merge
Browse files Browse the repository at this point in the history
Allow null properties for Onyx.merge
  • Loading branch information
deetergp authored Sep 19, 2023
2 parents bac801b + f71af27 commit f4e79cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
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,
};

0 comments on commit f4e79cd

Please sign in to comment.