Skip to content

Commit 8dc2435

Browse files
committed
fix: fix schema inferring type T for jsObject
1 parent 9b818e6 commit 8dc2435

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

src/morphism.ts

+27-34
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,6 @@
22
* @module morphism
33
*/
44
import { isString, get, isFunction, isObject, zipObject, isUndefined, assignInWith, aggregator } from './helpers';
5-
6-
/**
7-
* A String path that indicates where to find the property in the source input
8-
*
9-
* @example
10-
* ```typescript
11-
*
12-
* const source = {
13-
* foo: 'baz',
14-
* bar: ['bar', 'foo'],
15-
* baz: {
16-
* qux: 'bazqux'
17-
* }
18-
* };
19-
* const schema = {
20-
* foo: 'foo', // Simple Projection
21-
* bazqux: 'baz.qux' // Grab a value from a nested property
22-
* };
23-
*
24-
* morphism(schema, source);
25-
* //=> { foo: 'baz', bazqux: 'bazqux' }
26-
* ```
27-
*
28-
*/
29-
export type ActionString = string;
305
export type ActionFunction = {
316
/**
327
* A Function invoked per iteration
@@ -55,6 +30,30 @@ export type ActionFunction = {
5530
*/
5631
(iteratee: any, source: any | any[], target: any): any;
5732
};
33+
/**
34+
* A String path that indicates where to find the property in the source input
35+
*
36+
* @example
37+
* ```typescript
38+
*
39+
* const source = {
40+
* foo: 'baz',
41+
* bar: ['bar', 'foo'],
42+
* baz: {
43+
* qux: 'bazqux'
44+
* }
45+
* };
46+
* const schema = {
47+
* foo: 'foo', // Simple Projection
48+
* bazqux: 'baz.qux' // Grab a value from a nested property
49+
* };
50+
*
51+
* morphism(schema, source);
52+
* //=> { foo: 'baz', bazqux: 'bazqux' }
53+
* ```
54+
*
55+
*/
56+
export type ActionString = string;
5857
/**
5958
* An Array of String that allows to perform a function over source property
6059
*
@@ -140,12 +139,6 @@ export type Schema<Target> = {
140139
[destinationProperty in keyof Target]?: ActionString | ActionFunction | ActionAggregator | ActionSelector
141140
};
142141

143-
function transformValuesFromObject<TDestination, Source>(
144-
object: Source,
145-
schema: Schema<TDestination>,
146-
items: Source[],
147-
objectToCompute: TDestination
148-
): TDestination;
149142
/**
150143
* Low Level transformer function.
151144
* Take a plain object as input and transform its values using a specified schema.
@@ -154,7 +147,7 @@ function transformValuesFromObject<TDestination, Source>(
154147
* @param {Array} items Items to be forwarded to Actions
155148
* @param {} objectToCompute Created tranformed object of a given type
156149
*/
157-
function transformValuesFromObject<TDestination, Source>(
150+
function transformValuesFromObject<Source, TDestination>(
158151
object: Source,
159152
schema: Schema<TDestination>,
160153
items: Source[],
@@ -224,7 +217,7 @@ function transformItems<T, TSchema extends Schema<T>>(
224217
type: Constructable<T>
225218
): Mapper<{ [P in keyof TSchema]: any }>;
226219

227-
function transformItems<T, TSchema extends Schema<T>>(schema: TSchema, type?: Constructable<T>) {
220+
function transformItems<T, TSchema extends Schema<T | {}>>(schema: TSchema, type?: Constructable<T>) {
228221
function mapper(source: any): any {
229222
if (!source) {
230223
return source;
@@ -243,7 +236,7 @@ function transformItems<T, TSchema extends Schema<T>>(schema: TSchema, type?: Co
243236
const object = source;
244237
if (type) {
245238
const classObject = new type();
246-
return transformValuesFromObject(object, schema, [object], classObject);
239+
return transformValuesFromObject<any, T>(object, schema, [object], classObject);
247240
} else {
248241
const jsObject = {};
249242
return transformValuesFromObject(object, schema, [object], jsObject);

0 commit comments

Comments
 (0)