Skip to content

Commit

Permalink
Merge pull request #149 from nobrainr/fix/typescript-type-widening-on…
Browse files Browse the repository at this point in the history
…-action-function

Fix/typescript type widening on action function
  • Loading branch information
emyann authored Nov 11, 2019
2 parents 883c2b8 + 922f487 commit 438a168
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ 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>
| ActionFunction<Target, Source, Target[destinationProperty]>
| { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] }
| ActionAggregator<Source>
| ActionSelector<Source, Target>
| StrictSchema<Target[destinationProperty], Source>;
Expand All @@ -44,7 +44,7 @@ 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>
| ActionFunction<Target, Source, Target[destinationProperty]>
| { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] }
| ActionAggregator<Source>
| ActionSelector<Source, Target>
| Schema<Target[destinationProperty], Source>;
Expand Down
10 changes: 9 additions & 1 deletion src/typescript.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 438a168

Please sign in to comment.