From 6310583e0b86ec36c4c4fd763dae57071f474516 Mon Sep 17 00:00:00 2001 From: Yann Renaudin Date: Mon, 11 Nov 2019 18:29:29 -0500 Subject: [PATCH 1/2] fix: use expression instead of interface to avoid typescript widening type --- src/types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types.ts b/src/types.ts index 7227386..09cb5a4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -35,7 +35,7 @@ export type StrictSchema = { /** `destinationProperty` is the name of the property of the target object you want to produce */ [destinationProperty in keyof Target]: | ActionString - | ActionFunction + | { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] } | ActionAggregator | ActionSelector | StrictSchema; @@ -44,7 +44,7 @@ export type Schema = { /** `destinationProperty` is the name of the property of the target object you want to produce */ [destinationProperty in keyof Target]?: | ActionString - | ActionFunction + | { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] } | ActionAggregator | ActionSelector | Schema; From 922f487eb73b4a00b4c386a585a38209e8f99fbd Mon Sep 17 00:00:00 2001 From: Yann Renaudin Date: Mon, 11 Nov 2019 18:30:04 -0500 Subject: [PATCH 2/2] test: add typescript test to ensure union type as target is allowed --- src/typescript.spec.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/typescript.spec.ts b/src/typescript.spec.ts index daccd97..129eac4 100644 --- a/src/typescript.spec.ts +++ b/src/typescript.spec.ts @@ -1,4 +1,4 @@ -import Morphism, { morphism, StrictSchema, Schema } from './morphism'; +import Morphism, { morphism, StrictSchema, Schema, createSchema } from './morphism'; describe('Typescript', () => { describe('Registry Type Checking', () => { @@ -184,6 +184,14 @@ describe('Typescript', () => { expect(item).toEqual(expected[idx]); }); }); + + it('should accept union types as Target', () => { + const schema = createSchema<{ a: string } | { a: string; b: string }, { c: string }>({ + a: ({ c }) => c + }); + + expect(morphism(schema, { c: 'result' }).a).toEqual('result'); + }); }); describe('Morphism Function Type Checking', () => {