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(store): allow union of types in props #2301

Merged
merged 2 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions modules/store/spec/action_creator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ describe('Action Creators', () => {
narrow(foo({ foo: 42 }));
});

it('should allow the union of types in props', () => {
interface A {
sameProp: 'A';
}
interface B {
sameProp: 'B';
extraProp: string;
}
type U = A | B;
const foo = createAction('FOO', props<U>());

const fooA = foo({ sameProp: 'A' });
const fooB = foo({ sameProp: 'B', extraProp: 'allowed' });

expect(fooA).toEqual({ type: 'FOO', sameProp: 'A' });
expect(fooB).toEqual({
type: 'FOO',
sameProp: 'B',
extraProp: 'allowed',
});
});

it('should be serializable', () => {
const foo = createAction('FOO', props<{ foo: number }>());
const fooAction = foo({ foo: 42 });
Expand Down
13 changes: 5 additions & 8 deletions modules/store/src/action_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {
ActionCreator,
TypedAction,
FunctionWithParametersType,
PropsReturnType,
DisallowArraysAndTypeProperty,
NotAllowedCheck,
} from './models';

// Action creators taken from ts-action library and modified a bit to better
Expand All @@ -16,14 +15,14 @@ export function createAction<T extends string>(
export function createAction<T extends string, P extends object>(
type: T,
config: { _as: 'props'; _p: P }
): ActionCreator<T, (props: P) => P & TypedAction<T>>;
): ActionCreator<T, (props: P & NotAllowedCheck<P>) => P & TypedAction<T>>;
export function createAction<
T extends string,
P extends any[],
R extends object
>(
type: T,
creator: Creator<P, DisallowArraysAndTypeProperty<R>>
creator: Creator<P, R> & NotAllowedCheck<R>
): FunctionWithParametersType<P, R & TypedAction<T>> & TypedAction<T>;
/**
* @description
Expand Down Expand Up @@ -121,10 +120,8 @@ export function createAction<T extends string, C extends Creator>(
}
}

export function props<P extends object>(): PropsReturnType<P> {
// the return type does not match TypePropertyIsNotAllowed, so double casting
// is used.
return ({ _as: 'props', _p: undefined! } as unknown) as PropsReturnType<P>;
export function props<P extends object>(): { _as: 'props'; _p: P } {
alex-okrushko marked this conversation as resolved.
Show resolved Hide resolved
return { _as: 'props', _p: undefined! };
}

export function union<
Expand Down
12 changes: 3 additions & 9 deletions modules/store/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,11 @@ export type FunctionIsNotAllowed<
export type Creator<
P extends any[] = any[],
R extends object = object
> = R extends any[]
? ArraysAreNotAllowed
: R extends { type: any }
? TypePropertyIsNotAllowed
: FunctionWithParametersType<P, R>;
> = FunctionWithParametersType<P, R>;

export type PropsReturnType<T extends object> = T extends any[]
export type NotAllowedCheck<T extends object> = T extends any[]
alex-okrushko marked this conversation as resolved.
Show resolved Hide resolved
? ArraysAreNotAllowed
: T extends { type: any }
? TypePropertyIsNotAllowed
: { _as: 'props'; _p: T };
: T extends { type: any } ? TypePropertyIsNotAllowed : unknown;

/**
* See `Creator`.
Expand Down