react-native-onyx 1.0.97 → 1.0.98

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/Onyx.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import {Component} from 'react';
2
- import {PartialDeep} from 'type-fest';
3
2
  import * as Logger from './Logger';
4
- import {CollectionKey, CollectionKeyBase, DeepRecord, KeyValueMapping, OnyxCollection, OnyxEntry, OnyxKey, NullableProperties} from './types';
3
+ import {CollectionKey, CollectionKeyBase, DeepRecord, KeyValueMapping, NullishDeep, OnyxCollection, OnyxEntry, OnyxKey} from './types';
5
4
 
6
5
  /**
7
6
  * Represents a mapping object where each `OnyxKey` maps to either a value of its corresponding type in `KeyValueMapping` or `null`.
@@ -79,14 +78,14 @@ type OnyxUpdate =
79
78
  | {
80
79
  onyxMethod: typeof METHOD.MERGE;
81
80
  key: TKey;
82
- value: PartialDeep<KeyValueMapping[TKey]>;
81
+ value: NullishDeep<KeyValueMapping[TKey]>;
83
82
  };
84
83
  }[OnyxKey]
85
84
  | {
86
85
  [TKey in CollectionKeyBase]: {
87
86
  onyxMethod: typeof METHOD.MERGE_COLLECTION;
88
87
  key: TKey;
89
- value: Record<`${TKey}${string}`, PartialDeep<KeyValueMapping[TKey]>>;
88
+ value: Record<`${TKey}${string}`, NullishDeep<KeyValueMapping[TKey]>>;
90
89
  };
91
90
  }[CollectionKeyBase];
92
91
 
@@ -202,7 +201,7 @@ declare function multiSet(data: Partial<NullableKeyValueMapping>): Promise<void>
202
201
  * @param key ONYXKEYS key
203
202
  * @param value Object or Array value to merge
204
203
  */
205
- declare function merge<TKey extends OnyxKey>(key: TKey, value: NullableProperties<PartialDeep<KeyValueMapping[TKey]>>): Promise<void>;
204
+ declare function merge<TKey extends OnyxKey>(key: TKey, value: NullishDeep<KeyValueMapping[TKey]>): Promise<void>;
206
205
 
207
206
  /**
208
207
  * Clear out all the data in the store
@@ -246,7 +245,7 @@ declare function clear(keysToPreserve?: OnyxKey[]): Promise<void>;
246
245
  */
247
246
  declare function mergeCollection<TKey extends CollectionKeyBase, TMap>(
248
247
  collectionKey: TKey,
249
- collection: Collection<TKey, TMap, PartialDeep<KeyValueMapping[TKey]>>,
248
+ collection: Collection<TKey, TMap, NullishDeep<KeyValueMapping[TKey]>>,
250
249
  ): Promise<void>;
251
250
 
252
251
  /**
package/lib/types.d.ts CHANGED
@@ -1,10 +1,13 @@
1
1
  import {Merge} from 'type-fest';
2
+ import {BuiltIns} from 'type-fest/source/internal';
2
3
 
3
4
  /**
4
5
  * Represents a deeply nested record. It maps keys to values,
5
6
  * and those values can either be of type `TValue` or further nested `DeepRecord` instances.
6
7
  */
7
- type DeepRecord<TKey extends string | number | symbol, TValue> = {[key: string]: TValue | DeepRecord<TKey, TValue>};
8
+ type DeepRecord<TKey extends string | number | symbol, TValue> = {
9
+ [key: string]: TValue | DeepRecord<TKey, TValue>;
10
+ };
8
11
 
9
12
  /**
10
13
  * Represents type options to configure all Onyx methods.
@@ -180,14 +183,42 @@ type OnyxEntry<TOnyxValue> = TOnyxValue | null;
180
183
  */
181
184
  type OnyxCollection<TOnyxValue> = OnyxEntry<Record<string, TOnyxValue | null>>;
182
185
 
186
+ type NonTransformableTypes =
187
+ | BuiltIns
188
+ | ((...args: any[]) => unknown)
189
+ | Map<unknown, unknown>
190
+ | Set<unknown>
191
+ | ReadonlyMap<unknown, unknown>
192
+ | ReadonlySet<unknown>
193
+ | unknown[]
194
+ | readonly unknown[];
195
+
183
196
  /**
184
- * The `NullableProperties<T>` sets the values of all properties in `T` to be nullable (i.e., `| null`).
185
- * It doesn't recurse into nested property values, this means it applies the nullability only to the top-level properties.
197
+ * Create a type from another type with all keys and nested keys set to optional or null.
198
+ *
199
+ * @example
200
+ * const settings: Settings = {
201
+ * textEditor: {
202
+ * fontSize: 14;
203
+ * fontColor: '#000000';
204
+ * fontWeight: 400;
205
+ * }
206
+ * autosave: true;
207
+ * };
186
208
  *
187
- * @template T The type of the properties to convert to nullable properties.
209
+ * const applySavedSettings = (savedSettings: NullishDeep<Settings>) => {
210
+ * return {...settings, ...savedSettings};
211
+ * }
212
+ *
213
+ * settings = applySavedSettings({textEditor: {fontWeight: 500, fontColor: null}});
188
214
  */
189
- type NullableProperties<T> = {
190
- [P in keyof T]: T[P] | null;
215
+ type NullishDeep<T> = T extends NonTransformableTypes ? T : T extends object ? NullishObjectDeep<T> : unknown;
216
+
217
+ /**
218
+ Same as `NullishDeep`, but accepts only `object`s as inputs. Internal helper for `NullishDeep`.
219
+ */
220
+ type NullishObjectDeep<ObjectType extends object> = {
221
+ [KeyType in keyof ObjectType]?: NullishDeep<ObjectType[KeyType]> | null;
191
222
  };
192
223
 
193
224
  export {
@@ -201,5 +232,5 @@ export {
201
232
  OnyxEntry,
202
233
  OnyxKey,
203
234
  Selector,
204
- NullableProperties,
235
+ NullishDeep,
205
236
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-onyx",
3
- "version": "1.0.97",
3
+ "version": "1.0.98",
4
4
  "author": "Expensify, Inc.",
5
5
  "homepage": "https://expensify.com",
6
6
  "description": "State management for React Native",