Skip to content

Commit b5c2c8d

Browse files
committed
fix: mapper type fails on undefined source
1 parent c3aefad commit b5c2c8d

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/morphism.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function transformItems<T, TSchema extends Schema<T | {}>>(schema: TSchema, type
7272
tree = new MorphismSchemaTree(schema);
7373
}
7474

75-
function mapper(source: any) {
75+
const mapper: Mapper<TSchema> = (source: any) => {
7676
if (!source) {
7777
return source;
7878
}
@@ -96,7 +96,7 @@ function transformItems<T, TSchema extends Schema<T | {}>>(schema: TSchema, type
9696
return transformValuesFromObject(object, tree, [object], jsObject);
9797
}
9898
}
99-
}
99+
};
100100

101101
return mapper;
102102
}

src/types.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export type StrictSchema<Target = any, Source = any> = {
3838
| ActionFunction<Target, Source, Target[destinationProperty]>
3939
| ActionAggregator<Source>
4040
| ActionSelector<Source, Target[destinationProperty]>
41-
| StrictSchema<Target[destinationProperty], Source>
41+
| StrictSchema<Target[destinationProperty], Source>;
4242
} & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions<Target> };
4343
export type Schema<Target = any, Source = any> = {
4444
/** `destinationProperty` is the name of the property of the target object you want to produce */
@@ -47,7 +47,7 @@ export type Schema<Target = any, Source = any> = {
4747
| ActionFunction<Target, Source, Target[destinationProperty]>
4848
| ActionAggregator<Source>
4949
| ActionSelector<Source, Target[destinationProperty]>
50-
| Schema<Target[destinationProperty], Source>
50+
| Schema<Target[destinationProperty], Source>;
5151
} & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions<Target | any> };
5252

5353
export type Actions<Target, Source> = ActionFunction<Target, Source> | ActionAggregator | ActionString<Target> | ActionSelector<Source>;
@@ -172,6 +172,6 @@ export type DestinationFromSchema<T> = T extends StrictSchema<infer U> | Schema<
172172
export type ResultItem<TSchema extends Schema> = DestinationFromSchema<TSchema>;
173173

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

0 commit comments

Comments
 (0)