Skip to content

Commit

Permalink
fix: provide destination property to Selector Action to infer return …
Browse files Browse the repository at this point in the history
…type of fn
  • Loading branch information
emyann committed Dec 14, 2019
1 parent 438a168 commit 0665af2
Showing 1 changed file with 50 additions and 12 deletions.
62 changes: 50 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SCHEMA_OPTIONS_SYMBOL, SchemaOptions } from './morphism';
import { SCHEMA_OPTIONS_SYMBOL, SchemaOptions } from "./morphism";

/**
* A structure-preserving object from a source data towards a target data.
Expand Down Expand Up @@ -35,22 +35,38 @@ export type StrictSchema<Target = any, Source = any> = {
/** `destinationProperty` is the name of the property of the target object you want to produce */
[destinationProperty in keyof Target]:
| ActionString<Source>
| { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] }
| {
(
iteratee: Source,
source: Source[],
target: Target[destinationProperty]
): Target[destinationProperty];
}
| ActionAggregator<Source>
| ActionSelector<Source, Target>
| ActionSelector<Source, Target, destinationProperty>
| StrictSchema<Target[destinationProperty], Source>;
} & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions<Target> };
export type Schema<Target = any, Source = any> = {
/** `destinationProperty` is the name of the property of the target object you want to produce */
[destinationProperty in keyof Target]?:
| ActionString<Source>
| { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] }
| {
(
iteratee: Source,
source: Source[],
target: Target[destinationProperty]
): Target[destinationProperty];
}
| ActionAggregator<Source>
| ActionSelector<Source, Target>
| ActionSelector<Source, Target, destinationProperty>
| Schema<Target[destinationProperty], Source>;
} & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions<Target | any> };

export type Actions<Target, Source> = ActionFunction<Target, Source> | ActionAggregator | ActionString<Target> | ActionSelector<Source>;
export type Actions<Target, Source> =
| ActionFunction<Target, Source>
| ActionAggregator
| ActionString<Target>
| ActionSelector<Source>;

/**
* @interface ActionFunction
Expand Down Expand Up @@ -129,7 +145,9 @@ export type ActionString<Source> = string; // TODO: ActionString should support
* //=> { fooAndBar: { foo: 'foo', bar: 'bar' } }
* ```
*/
export type ActionAggregator<T extends unknown = unknown> = T extends object ? (keyof T)[] | string[] : string[];
export type ActionAggregator<T extends unknown = unknown> = T extends object
? (keyof T)[] | string[]
: string[];
/**
* @interface ActionSelector
* @typeparam Source Source/Input Type
Expand Down Expand Up @@ -157,21 +175,41 @@ export type ActionAggregator<T extends unknown = unknown> = T extends object ? (
*```
*
*/
export interface ActionSelector<Source = object, R = any> {
export interface ActionSelector<
Source = object,
Target = any,
TargetProperty extends keyof Target = any
> {
path: ActionString<Source> | ActionAggregator<Source>;
fn: (fieldValue: any, object: Source, items: Source, objectToCompute: R) => R;
fn: (
fieldValue: any,
object: Source,
items: Source,
objectToCompute: Target
) => Target[TargetProperty];
}

export interface Constructable<T> {
new (...args: any[]): T;
}

export type SourceFromSchema<T> = T extends StrictSchema<unknown, infer U> | Schema<unknown, infer U> ? U : never;
export type DestinationFromSchema<T> = T extends StrictSchema<infer U> | Schema<infer U> ? U : never;
export type SourceFromSchema<T> = T extends
| StrictSchema<unknown, infer U>
| Schema<unknown, infer U>
? U
: never;
export type DestinationFromSchema<T> = T extends
| StrictSchema<infer U>
| Schema<infer U>
? U
: never;

export type ResultItem<TSchema extends Schema> = DestinationFromSchema<TSchema>;

export interface Mapper<TSchema extends Schema | StrictSchema, TResult = ResultItem<TSchema>> {
export interface Mapper<
TSchema extends Schema | StrictSchema,
TResult = ResultItem<TSchema>
> {
(data?: SourceFromSchema<TSchema>[] | null): TResult[];
(data?: SourceFromSchema<TSchema> | null): TResult;
}

0 comments on commit 0665af2

Please sign in to comment.