From 343d7d9d1234bd2f29ee3be1c55d91947fab9b0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Henriques?= Date: Thu, 27 Jul 2023 14:40:15 +0100 Subject: [PATCH] Second attempt to solve collection key issue --- lib/types.d.ts | 2 +- lib/withOnyx.d.ts | 73 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/lib/types.d.ts b/lib/types.d.ts index d74cbe82..a61c5706 100644 --- a/lib/types.d.ts +++ b/lib/types.d.ts @@ -40,4 +40,4 @@ type Selector = IsEqual = IsEqual, unknown> extends true ? (value: unknown) => unknown : (value: GetOnyxValue | null) => TReturnType; -export {CollectionKey, CustomTypeOptions, DeepRecord, Key, MergeBy, OnyxKey, KeyValueMapping, Selector, GetOnyxValue, TypeOptions}; +export {CollectionKey, CollectionKeyBase, CustomTypeOptions, DeepRecord, Key, MergeBy, OnyxKey, KeyValueMapping, Selector, GetOnyxValue, TypeOptions}; diff --git a/lib/withOnyx.d.ts b/lib/withOnyx.d.ts index 95b06b0c..faedbc56 100644 --- a/lib/withOnyx.d.ts +++ b/lib/withOnyx.d.ts @@ -1,14 +1,17 @@ import {IsEqual} from 'type-fest'; -import {GetOnyxValue, OnyxKey, Selector} from './types'; +import {CollectionKey, CollectionKeyBase, GetOnyxValue, Key, KeyValueMapping, OnyxKey, Selector} from './types'; -type BaseMapping = { +type BaseMapping = { canEvict?: boolean | ((props: Omit) => boolean); initWithStoredValues?: boolean; + // TODO: Remove this when types are finished. + onyxValue?: GetOnyxValue | null; }; -type Mapping = BaseMapping & +// TODO: Still being worked on. +type Mapping = BaseMapping & ( - | (IsEqual | null, TOnyxProps[TOnyxProp]> extends true + | (IsEqual extends true ? { key: TOnyxKey | ((props: Omit) => TOnyxKey); } @@ -23,14 +26,70 @@ type Mapping = BaseMapping & + ( + | (IsEqual | null, TOnyxProps[TOnyxProp]> extends true + ? { + key: TOnyxKey | ((props: Omit) => TOnyxKey); + } + : never) + | { + key: TOnyxKey; + selector: Selector; + } + | { + key: (props: Omit) => TOnyxKey; + selector: Selector; + } + ); + +// TODO: Still being worked on. +type CollectionRecordMapping = BaseMapping< + TComponentProps, + TOnyxProps, + TOnyxProp, + TOnyxKey +> & + ( + | (IsEqual extends true + ? { + key: TOnyxKey | ((props: Omit) => TOnyxKey); + } + : never) + | { + key: TOnyxKey; + selector: Selector; + } + | { + key: (props: Omit) => TOnyxKey; + selector: Selector; + } + ); + +// TODO: Still being worked on. type OnyxPropMapping = { - [TOnyxKey in OnyxKey]: Mapping; -}[OnyxKey]; + [TOnyxKey in Key]: Mapping; +}[Key]; + +// TODO: Still being worked on. +type OnyxPropCollectionMapping = { + [TOnyxKey in CollectionKeyBase]: CollectionMapping; +}[CollectionKeyBase]; + +// TODO: Still being worked on. +type OnyxPropCollectionRecordMapping = { + [TOnyxKey in CollectionKey]: CollectionRecordMapping; +}[CollectionKey]; declare function withOnyx( mapping: { - [TOnyxProp in keyof TOnyxProps]: OnyxPropMapping; + [TOnyxProp in keyof TOnyxProps]: + | OnyxPropMapping + | OnyxPropCollectionMapping + | OnyxPropCollectionRecordMapping; }, ): (component: React.ComponentType) => React.ComponentType>; export default withOnyx; +export {Mapping, CollectionMapping, CollectionRecordMapping, OnyxPropMapping, GetOnyxValue, Selector};