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

[TS] Allow deep null values on merge operations #381

Merged
merged 1 commit into from
Oct 3, 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
11 changes: 5 additions & 6 deletions lib/Onyx.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Component} from 'react';
import {PartialDeep} from 'type-fest';
import * as Logger from './Logger';
import {CollectionKey, CollectionKeyBase, DeepRecord, KeyValueMapping, OnyxCollection, OnyxEntry, OnyxKey, NullableProperties} from './types';
import {CollectionKey, CollectionKeyBase, DeepRecord, KeyValueMapping, NullishDeep, OnyxCollection, OnyxEntry, OnyxKey} 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 @@ -79,14 +78,14 @@ type OnyxUpdate =
| {
onyxMethod: typeof METHOD.MERGE;
key: TKey;
value: PartialDeep<KeyValueMapping[TKey]>;
value: NullishDeep<KeyValueMapping[TKey]>;
};
}[OnyxKey]
| {
[TKey in CollectionKeyBase]: {
onyxMethod: typeof METHOD.MERGE_COLLECTION;
key: TKey;
value: Record<`${TKey}${string}`, PartialDeep<KeyValueMapping[TKey]>>;
value: Record<`${TKey}${string}`, NullishDeep<KeyValueMapping[TKey]>>;
};
}[CollectionKeyBase];

Expand Down Expand Up @@ -202,7 +201,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: NullableProperties<PartialDeep<KeyValueMapping[TKey]>>): Promise<void>;
declare function merge<TKey extends OnyxKey>(key: TKey, value: NullishDeep<KeyValueMapping[TKey]>): Promise<void>;

/**
* Clear out all the data in the store
Expand Down Expand Up @@ -246,7 +245,7 @@ declare function clear(keysToPreserve?: OnyxKey[]): Promise<void>;
*/
declare function mergeCollection<TKey extends CollectionKeyBase, TMap>(
collectionKey: TKey,
collection: Collection<TKey, TMap, PartialDeep<KeyValueMapping[TKey]>>,
collection: Collection<TKey, TMap, NullishDeep<KeyValueMapping[TKey]>>,
): Promise<void>;

/**
Expand Down
45 changes: 38 additions & 7 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {Merge} from 'type-fest';
import {BuiltIns} from 'type-fest/source/internal';

/**
* Represents a deeply nested record. It maps keys to values,
* and those values can either be of type `TValue` or further nested `DeepRecord` instances.
*/
type DeepRecord<TKey extends string | number | symbol, TValue> = {[key: string]: TValue | DeepRecord<TKey, TValue>};
type DeepRecord<TKey extends string | number | symbol, TValue> = {
[key: string]: TValue | DeepRecord<TKey, TValue>;
};

/**
* Represents type options to configure all Onyx methods.
Expand Down Expand Up @@ -180,14 +183,42 @@ type OnyxEntry<TOnyxValue> = TOnyxValue | null;
*/
type OnyxCollection<TOnyxValue> = OnyxEntry<Record<string, TOnyxValue | null>>;

type NonTransformableTypes =
| BuiltIns
| ((...args: any[]) => unknown)
| Map<unknown, unknown>
| Set<unknown>
| ReadonlyMap<unknown, unknown>
| ReadonlySet<unknown>
| unknown[]
| readonly unknown[];

/**
* 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.
* Create a type from another type with all keys and nested keys set to optional or null.
*
* @example
* const settings: Settings = {
* textEditor: {
* fontSize: 14;
* fontColor: '#000000';
* fontWeight: 400;
* }
* autosave: true;
* };
*
* @template T The type of the properties to convert to nullable properties.
* const applySavedSettings = (savedSettings: NullishDeep<Settings>) => {
* return {...settings, ...savedSettings};
* }
*
* settings = applySavedSettings({textEditor: {fontWeight: 500, fontColor: null}});
*/
type NullableProperties<T> = {
[P in keyof T]: T[P] | null;
type NullishDeep<T> = T extends NonTransformableTypes ? T : T extends object ? NullishObjectDeep<T> : unknown;

/**
Same as `NullishDeep`, but accepts only `object`s as inputs. Internal helper for `NullishDeep`.
*/
type NullishObjectDeep<ObjectType extends object> = {
[KeyType in keyof ObjectType]?: NullishDeep<ObjectType[KeyType]> | null;
};

export {
Expand All @@ -201,5 +232,5 @@ export {
OnyxEntry,
OnyxKey,
Selector,
NullableProperties,
NullishDeep,
};
Loading