Skip to content

Commit 70f769d

Browse files
committed
Refactor OnyxUpdate type to bring back type safety
1 parent 6a24680 commit 70f769d

1 file changed

Lines changed: 38 additions & 33 deletions

File tree

lib/types.ts

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -326,44 +326,49 @@ type OnyxMergeCollectionInput<TKey extends OnyxKey, TMap = object> = Collection<
326326

327327
type OnyxMethodMap = typeof OnyxUtils.METHOD;
328328

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-
};
356-
357329
/**
358330
* OnyxUpdate type includes all onyx methods used in OnyxMethodValueMap.
359331
* If a new method is added to OnyxUtils.METHOD constant, it must be added to OnyxMethodValueMap type.
360332
* Otherwise it will show static type errors.
361333
*/
362-
type OnyxUpdate = {
363-
[Method in OnyxMethod]: {
364-
onyxMethod: Method;
365-
} & OnyxMethodValueMap[Method];
366-
}[OnyxMethod];
334+
type OnyxUpdate =
335+
// ⚠️ DO NOT CHANGE THIS TYPE, UNLESS YOU KNOW WHAT YOU ARE DOING. ⚠️
336+
| {
337+
[TKey in OnyxKey]:
338+
| {
339+
onyxMethod: typeof OnyxUtils.METHOD.SET;
340+
key: TKey;
341+
value: OnyxSetInput<TKey>;
342+
}
343+
| {
344+
onyxMethod: typeof OnyxUtils.METHOD.MULTI_SET;
345+
key: TKey;
346+
value: OnyxMultiSetInput;
347+
}
348+
| {
349+
onyxMethod: typeof OnyxUtils.METHOD.MERGE;
350+
key: TKey;
351+
value: OnyxMergeInput<TKey>;
352+
}
353+
| {
354+
onyxMethod: typeof OnyxUtils.METHOD.CLEAR;
355+
key: TKey;
356+
value?: undefined;
357+
};
358+
}[OnyxKey]
359+
| {
360+
[TKey in CollectionKeyBase]:
361+
| {
362+
onyxMethod: typeof OnyxUtils.METHOD.MERGE_COLLECTION;
363+
key: TKey;
364+
value: OnyxMergeCollectionInput<TKey>;
365+
}
366+
| {
367+
onyxMethod: typeof OnyxUtils.METHOD.SET_COLLECTION;
368+
key: TKey;
369+
value: OnyxMergeCollectionInput<TKey>;
370+
};
371+
}[CollectionKeyBase];
367372

368373
/**
369374
* Represents the options used in `Onyx.set()` method.

0 commit comments

Comments
 (0)