Skip to content

Commit 438a168

Browse files
authored
Merge pull request #149 from nobrainr/fix/typescript-type-widening-on-action-function
Fix/typescript type widening on action function
2 parents 883c2b8 + 922f487 commit 438a168

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export type StrictSchema<Target = any, Source = any> = {
3535
/** `destinationProperty` is the name of the property of the target object you want to produce */
3636
[destinationProperty in keyof Target]:
3737
| ActionString<Source>
38-
| ActionFunction<Target, Source, Target[destinationProperty]>
38+
| { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] }
3939
| ActionAggregator<Source>
4040
| ActionSelector<Source, Target>
4141
| StrictSchema<Target[destinationProperty], Source>;
@@ -44,7 +44,7 @@ export type Schema<Target = any, Source = any> = {
4444
/** `destinationProperty` is the name of the property of the target object you want to produce */
4545
[destinationProperty in keyof Target]?:
4646
| ActionString<Source>
47-
| ActionFunction<Target, Source, Target[destinationProperty]>
47+
| { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] }
4848
| ActionAggregator<Source>
4949
| ActionSelector<Source, Target>
5050
| Schema<Target[destinationProperty], Source>;

src/typescript.spec.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Morphism, { morphism, StrictSchema, Schema } from './morphism';
1+
import Morphism, { morphism, StrictSchema, Schema, createSchema } from './morphism';
22

33
describe('Typescript', () => {
44
describe('Registry Type Checking', () => {
@@ -184,6 +184,14 @@ describe('Typescript', () => {
184184
expect(item).toEqual(expected[idx]);
185185
});
186186
});
187+
188+
it('should accept union types as Target', () => {
189+
const schema = createSchema<{ a: string } | { a: string; b: string }, { c: string }>({
190+
a: ({ c }) => c
191+
});
192+
193+
expect(morphism(schema, { c: 'result' }).a).toEqual('result');
194+
});
187195
});
188196

189197
describe('Morphism Function Type Checking', () => {

0 commit comments

Comments
 (0)