Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/typescript type widening on action function #149

Merged
merged 2 commits into from
Nov 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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>;
@@ -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>;
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', () => {
@@ -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', () => {