Skip to content

Commit

Permalink
Second attempt to solve collection key issue
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioh8010 committed Jul 27, 2023
1 parent 4332d2f commit 343d7d9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ type Selector<TKey extends OnyxKey, TReturnType> = IsEqual<KeyValueMapping[TKey]

// type Selector<TKey extends OnyxKey, TReturnType> = IsEqual<GetOnyxValue<TKey>, unknown> extends true ? (value: unknown) => unknown : (value: GetOnyxValue<TKey> | 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};
73 changes: 66 additions & 7 deletions lib/withOnyx.d.ts
Original file line number Diff line number Diff line change
@@ -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<TComponentProps, TOnyxProps> = {
type BaseMapping<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps, TOnyxKey extends OnyxKey> = {
canEvict?: boolean | ((props: Omit<TComponentProps, keyof TOnyxProps>) => boolean);
initWithStoredValues?: boolean;
// TODO: Remove this when types are finished.
onyxValue?: GetOnyxValue<TOnyxKey> | null;
};

type Mapping<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps, TOnyxKey extends OnyxKey> = BaseMapping<TComponentProps, TOnyxProps> &
// TODO: Still being worked on.
type Mapping<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps, TOnyxKey extends Key> = BaseMapping<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey> &
(
| (IsEqual<GetOnyxValue<TOnyxKey> | null, TOnyxProps[TOnyxProp]> extends true
| (IsEqual<KeyValueMapping[TOnyxKey] | null, TOnyxProps[TOnyxProp]> extends true
? {
key: TOnyxKey | ((props: Omit<TComponentProps, keyof TOnyxProps>) => TOnyxKey);
}
Expand All @@ -23,14 +26,70 @@ type Mapping<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps, TO
}
);

// TODO: Still being worked on.
type CollectionMapping<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps, TOnyxKey extends CollectionKeyBase> = BaseMapping<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey> &
(
| (IsEqual<Record<string, KeyValueMapping[TOnyxKey] | null> | null, TOnyxProps[TOnyxProp]> extends true
? {
key: TOnyxKey | ((props: Omit<TComponentProps, keyof TOnyxProps>) => TOnyxKey);
}
: never)
| {
key: TOnyxKey;
selector: Selector<TOnyxKey, TOnyxProps[TOnyxProp]>;
}
| {
key: (props: Omit<TComponentProps, keyof TOnyxProps>) => TOnyxKey;
selector: Selector<TOnyxKey, TOnyxProps[TOnyxProp]>;
}
);

// TODO: Still being worked on.
type CollectionRecordMapping<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps, TOnyxKey extends CollectionKey> = BaseMapping<
TComponentProps,
TOnyxProps,
TOnyxProp,
TOnyxKey
> &
(
| (IsEqual<KeyValueMapping[TOnyxKey] | null, TOnyxProps[TOnyxProp]> extends true
? {
key: TOnyxKey | ((props: Omit<TComponentProps, keyof TOnyxProps>) => TOnyxKey);
}
: never)
| {
key: TOnyxKey;
selector: Selector<TOnyxKey, TOnyxProps[TOnyxProp]>;
}
| {
key: (props: Omit<TComponentProps, keyof TOnyxProps>) => TOnyxKey;
selector: Selector<TOnyxKey, TOnyxProps[TOnyxProp]>;
}
);

// TODO: Still being worked on.
type OnyxPropMapping<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps> = {
[TOnyxKey in OnyxKey]: Mapping<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey>;
}[OnyxKey];
[TOnyxKey in Key]: Mapping<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey>;
}[Key];

// TODO: Still being worked on.
type OnyxPropCollectionMapping<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps> = {
[TOnyxKey in CollectionKeyBase]: CollectionMapping<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey>;
}[CollectionKeyBase];

// TODO: Still being worked on.
type OnyxPropCollectionRecordMapping<TComponentProps, TOnyxProps, TOnyxProp extends keyof TOnyxProps> = {
[TOnyxKey in CollectionKey]: CollectionRecordMapping<TComponentProps, TOnyxProps, TOnyxProp, TOnyxKey>;
}[CollectionKey];

declare function withOnyx<TComponentProps, TOnyxProps>(
mapping: {
[TOnyxProp in keyof TOnyxProps]: OnyxPropMapping<TComponentProps, TOnyxProps, TOnyxProp>;
[TOnyxProp in keyof TOnyxProps]:
| OnyxPropMapping<TComponentProps, TOnyxProps, TOnyxProp>
| OnyxPropCollectionMapping<TComponentProps, TOnyxProps, TOnyxProp>
| OnyxPropCollectionRecordMapping<TComponentProps, TOnyxProps, TOnyxProp>;
},
): (component: React.ComponentType<TComponentProps>) => React.ComponentType<Omit<TComponentProps, keyof TOnyxProps>>;

export default withOnyx;
export {Mapping, CollectionMapping, CollectionRecordMapping, OnyxPropMapping, GetOnyxValue, Selector};

0 comments on commit 343d7d9

Please sign in to comment.