Skip to content

Commit 0665af2

Browse files
committed
fix: provide destination property to Selector Action to infer return type of fn
1 parent 438a168 commit 0665af2

File tree

1 file changed

+50
-12
lines changed

1 file changed

+50
-12
lines changed

src/types.ts

+50-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SCHEMA_OPTIONS_SYMBOL, SchemaOptions } from './morphism';
1+
import { SCHEMA_OPTIONS_SYMBOL, SchemaOptions } from "./morphism";
22

33
/**
44
* A structure-preserving object from a source data towards a target data.
@@ -35,22 +35,38 @@ export type StrictSchema<Target = any, Source = any> = {
3535
/** `destinationProperty` is the name of the property of the target object you want to produce */
3636
[destinationProperty in keyof Target]:
3737
| ActionString<Source>
38-
| { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] }
38+
| {
39+
(
40+
iteratee: Source,
41+
source: Source[],
42+
target: Target[destinationProperty]
43+
): Target[destinationProperty];
44+
}
3945
| ActionAggregator<Source>
40-
| ActionSelector<Source, Target>
46+
| ActionSelector<Source, Target, destinationProperty>
4147
| StrictSchema<Target[destinationProperty], Source>;
4248
} & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions<Target> };
4349
export type Schema<Target = any, Source = any> = {
4450
/** `destinationProperty` is the name of the property of the target object you want to produce */
4551
[destinationProperty in keyof Target]?:
4652
| ActionString<Source>
47-
| { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] }
53+
| {
54+
(
55+
iteratee: Source,
56+
source: Source[],
57+
target: Target[destinationProperty]
58+
): Target[destinationProperty];
59+
}
4860
| ActionAggregator<Source>
49-
| ActionSelector<Source, Target>
61+
| ActionSelector<Source, Target, destinationProperty>
5062
| Schema<Target[destinationProperty], Source>;
5163
} & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions<Target | any> };
5264

53-
export type Actions<Target, Source> = ActionFunction<Target, Source> | ActionAggregator | ActionString<Target> | ActionSelector<Source>;
65+
export type Actions<Target, Source> =
66+
| ActionFunction<Target, Source>
67+
| ActionAggregator
68+
| ActionString<Target>
69+
| ActionSelector<Source>;
5470

5571
/**
5672
* @interface ActionFunction
@@ -129,7 +145,9 @@ export type ActionString<Source> = string; // TODO: ActionString should support
129145
* //=> { fooAndBar: { foo: 'foo', bar: 'bar' } }
130146
* ```
131147
*/
132-
export type ActionAggregator<T extends unknown = unknown> = T extends object ? (keyof T)[] | string[] : string[];
148+
export type ActionAggregator<T extends unknown = unknown> = T extends object
149+
? (keyof T)[] | string[]
150+
: string[];
133151
/**
134152
* @interface ActionSelector
135153
* @typeparam Source Source/Input Type
@@ -157,21 +175,41 @@ export type ActionAggregator<T extends unknown = unknown> = T extends object ? (
157175
*```
158176
*
159177
*/
160-
export interface ActionSelector<Source = object, R = any> {
178+
export interface ActionSelector<
179+
Source = object,
180+
Target = any,
181+
TargetProperty extends keyof Target = any
182+
> {
161183
path: ActionString<Source> | ActionAggregator<Source>;
162-
fn: (fieldValue: any, object: Source, items: Source, objectToCompute: R) => R;
184+
fn: (
185+
fieldValue: any,
186+
object: Source,
187+
items: Source,
188+
objectToCompute: Target
189+
) => Target[TargetProperty];
163190
}
164191

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

169-
export type SourceFromSchema<T> = T extends StrictSchema<unknown, infer U> | Schema<unknown, infer U> ? U : never;
170-
export type DestinationFromSchema<T> = T extends StrictSchema<infer U> | Schema<infer U> ? U : never;
196+
export type SourceFromSchema<T> = T extends
197+
| StrictSchema<unknown, infer U>
198+
| Schema<unknown, infer U>
199+
? U
200+
: never;
201+
export type DestinationFromSchema<T> = T extends
202+
| StrictSchema<infer U>
203+
| Schema<infer U>
204+
? U
205+
: never;
171206

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

174-
export interface Mapper<TSchema extends Schema | StrictSchema, TResult = ResultItem<TSchema>> {
209+
export interface Mapper<
210+
TSchema extends Schema | StrictSchema,
211+
TResult = ResultItem<TSchema>
212+
> {
175213
(data?: SourceFromSchema<TSchema>[] | null): TResult[];
176214
(data?: SourceFromSchema<TSchema> | null): TResult;
177215
}

0 commit comments

Comments
 (0)