|
1 | 1 | import type {Merge} from 'type-fest'; |
2 | | -import type {BuiltIns} from 'type-fest/source/internal'; |
3 | 2 | import type OnyxUtils from './OnyxUtils'; |
4 | 3 | import type {OnyxMethod} from './OnyxUtils'; |
5 | 4 | import type {FastMergeReplaceNullPatch} from './utils'; |
@@ -157,6 +156,10 @@ type OnyxValue<TKey extends OnyxKey> = string extends TKey ? unknown : TKey exte |
157 | 156 | /** Utility type to extract `TOnyxValue` from `OnyxCollection<TOnyxValue>` */ |
158 | 157 | type ExtractOnyxCollectionValue<TOnyxCollection> = TOnyxCollection extends NonNullable<OnyxCollection<infer U>> ? U : never; |
159 | 158 |
|
| 159 | +type Primitive = null | undefined | string | number | boolean | symbol | bigint; |
| 160 | + |
| 161 | +type BuiltIns = Primitive | void | Date | RegExp; |
| 162 | + |
160 | 163 | type NonTransformableTypes = |
161 | 164 | | BuiltIns |
162 | 165 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
@@ -205,13 +208,7 @@ type NullishObjectDeep<ObjectType extends object> = { |
205 | 208 | * Also, the `TMap` type is inferred automatically in `mergeCollection()` method and represents |
206 | 209 | * the object of collection keys/values specified in the second parameter of the method. |
207 | 210 | */ |
208 | | -type Collection<TKey extends CollectionKeyBase, TValue, TMap = never> = { |
209 | | - [MapK in keyof TMap]: MapK extends `${TKey}${string}` |
210 | | - ? MapK extends `${TKey}` |
211 | | - ? never // forbids empty id |
212 | | - : TValue |
213 | | - : never; |
214 | | -}; |
| 211 | +type Collection<TKey extends CollectionKeyBase, TValue> = Record<`${TKey}${string}`, TValue> & {[P in TKey]?: never}; |
215 | 212 |
|
216 | 213 | /** Represents the base options used in `Onyx.connect()` method. */ |
217 | 214 | // NOTE: Any changes to this type like adding or removing options must be accounted in OnyxConnectionManager's `generateConnectionID()` method! |
@@ -322,48 +319,58 @@ type OnyxMergeInput<TKey extends OnyxKey> = OnyxInput<TKey>; |
322 | 319 | /** |
323 | 320 | * This represents the value that can be passed to `Onyx.merge` and to `Onyx.update` with the method "MERGE" |
324 | 321 | */ |
325 | | -type OnyxMergeCollectionInput<TKey extends OnyxKey, TMap = object> = Collection<TKey, NonNullable<OnyxInput<TKey>>, TMap>; |
| 322 | +type OnyxMergeCollectionInput<TKey extends OnyxKey> = Collection<TKey, NonNullable<OnyxInput<TKey>>>; |
326 | 323 |
|
327 | | -type OnyxMethodMap = typeof OnyxUtils.METHOD; |
| 324 | +/** |
| 325 | + * This represents the value that can be passed to `Onyx.setCollection` and to `Onyx.update` with the method "SET_COLLECTION" |
| 326 | + */ |
| 327 | +type OnyxSetCollectionInput<TKey extends OnyxKey> = Collection<TKey, OnyxInput<TKey>>; |
328 | 328 |
|
329 | | -// Maps onyx methods to their corresponding value types |
330 | | -type OnyxMethodValueMap = { |
331 | | - [OnyxUtils.METHOD.SET]: { |
332 | | - key: OnyxKey; |
333 | | - value: OnyxSetInput<OnyxKey>; |
334 | | - }; |
335 | | - [OnyxUtils.METHOD.MULTI_SET]: { |
336 | | - key: OnyxKey; |
337 | | - value: OnyxMultiSetInput; |
338 | | - }; |
339 | | - [OnyxUtils.METHOD.MERGE]: { |
340 | | - key: OnyxKey; |
341 | | - value: OnyxMergeInput<OnyxKey>; |
342 | | - }; |
343 | | - [OnyxUtils.METHOD.CLEAR]: { |
344 | | - key: OnyxKey; |
345 | | - value?: undefined; |
346 | | - }; |
347 | | - [OnyxUtils.METHOD.MERGE_COLLECTION]: { |
348 | | - key: CollectionKeyBase; |
349 | | - value: OnyxMergeCollectionInput<CollectionKeyBase>; |
350 | | - }; |
351 | | - [OnyxUtils.METHOD.SET_COLLECTION]: { |
352 | | - key: CollectionKeyBase; |
353 | | - value: OnyxMergeCollectionInput<CollectionKeyBase>; |
354 | | - }; |
355 | | -}; |
| 329 | +type OnyxMethodMap = typeof OnyxUtils.METHOD; |
356 | 330 |
|
357 | 331 | /** |
358 | 332 | * OnyxUpdate type includes all onyx methods used in OnyxMethodValueMap. |
359 | 333 | * If a new method is added to OnyxUtils.METHOD constant, it must be added to OnyxMethodValueMap type. |
360 | 334 | * Otherwise it will show static type errors. |
361 | 335 | */ |
362 | | -type OnyxUpdate = { |
363 | | - [Method in OnyxMethod]: { |
364 | | - onyxMethod: Method; |
365 | | - } & OnyxMethodValueMap[Method]; |
366 | | -}[OnyxMethod]; |
| 336 | +type OnyxUpdate = |
| 337 | + // ⚠️ DO NOT CHANGE THIS TYPE, UNLESS YOU KNOW WHAT YOU ARE DOING. ⚠️ |
| 338 | + | { |
| 339 | + [TKey in OnyxKey]: |
| 340 | + | { |
| 341 | + onyxMethod: typeof OnyxUtils.METHOD.SET; |
| 342 | + key: TKey; |
| 343 | + value: OnyxSetInput<TKey>; |
| 344 | + } |
| 345 | + | { |
| 346 | + onyxMethod: typeof OnyxUtils.METHOD.MULTI_SET; |
| 347 | + key: TKey; |
| 348 | + value: OnyxMultiSetInput; |
| 349 | + } |
| 350 | + | { |
| 351 | + onyxMethod: typeof OnyxUtils.METHOD.MERGE; |
| 352 | + key: TKey; |
| 353 | + value: OnyxMergeInput<TKey>; |
| 354 | + } |
| 355 | + | { |
| 356 | + onyxMethod: typeof OnyxUtils.METHOD.CLEAR; |
| 357 | + key: TKey; |
| 358 | + value?: undefined; |
| 359 | + }; |
| 360 | + }[OnyxKey] |
| 361 | + | { |
| 362 | + [TKey in CollectionKeyBase]: |
| 363 | + | { |
| 364 | + onyxMethod: typeof OnyxUtils.METHOD.MERGE_COLLECTION; |
| 365 | + key: TKey; |
| 366 | + value: OnyxMergeCollectionInput<TKey>; |
| 367 | + } |
| 368 | + | { |
| 369 | + onyxMethod: typeof OnyxUtils.METHOD.SET_COLLECTION; |
| 370 | + key: TKey; |
| 371 | + value: OnyxSetCollectionInput<TKey>; |
| 372 | + }; |
| 373 | + }[CollectionKeyBase]; |
367 | 374 |
|
368 | 375 | /** |
369 | 376 | * Represents the options used in `Onyx.set()` method. |
@@ -499,6 +506,7 @@ export type { |
499 | 506 | OnyxMultiSetInput, |
500 | 507 | OnyxMergeInput, |
501 | 508 | OnyxMergeCollectionInput, |
| 509 | + OnyxSetCollectionInput, |
502 | 510 | OnyxMethod, |
503 | 511 | OnyxMethodMap, |
504 | 512 | OnyxUpdate, |
|
0 commit comments