Skip to content

Commit

Permalink
fix(store): disallow arrays in action creators (#2155)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguttandin authored and brandonroberts committed Oct 14, 2019
1 parent 881c6bd commit 1e4c0be
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
20 changes: 18 additions & 2 deletions modules/store/spec/action_creator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,20 @@ describe('Action Creators', () => {
const value = fooAction.bar;
`).toFail(/'bar' does not exist on type/);
});

it('should not allow type property', () => {
expectSnippet(`
const foo = createAction('FOO', (type: string) => ({type}));
`).toFail(
/Type '{ type: string; }' is not assignable to type '"type property is not allowed in action creators"/
/Type '{ type: string; }' is not assignable to type '"type property is not allowed in action creators"'/
);
});

it('should not allow ararys', () => {
expectSnippet(`
const foo = createAction('FOO', () => [ ]);
`).toFail(
/Type 'any\[]' is not assignable to type '"arrays are not allowed in action creators"'/
);
});
});
Expand Down Expand Up @@ -150,12 +159,19 @@ describe('Action Creators', () => {
});

it('should not allow type property', () => {
const foo = createAction('FOO', props<{ type: number }>() as any);
expectSnippet(`
const foo = createAction('FOO', props<{ type: number }>());
`).toFail(
/Argument of type '"type property is not allowed in action creators"' is not assignable to parameter of type/
);
});

it('should not allow ararys', () => {
expectSnippet(`
const foo = createAction('FOO', props<[]>());
`).toFail(
/Argument of type '"arrays are not allowed in action creators"' is not assignable to parameter of type/
);
});
});
});
4 changes: 2 additions & 2 deletions modules/store/src/action_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
TypedAction,
FunctionWithParametersType,
PropsReturnType,
DisallowTypeProperty,
DisallowArraysAndTypeProperty,
} from './models';

// Action creators taken from ts-action library and modified a bit to better
Expand All @@ -23,7 +23,7 @@ export function createAction<
R extends object
>(
type: T,
creator: Creator<P, DisallowTypeProperty<R>>
creator: Creator<P, DisallowArraysAndTypeProperty<R>>
): FunctionWithParametersType<P, R & TypedAction<T>> & TypedAction<T>;
/**
* @description
Expand Down
28 changes: 18 additions & 10 deletions modules/store/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ export type SelectorWithProps<State, Props, Result> = (
props: Props
) => Result;

export type DisallowTypeProperty<T> = T extends { type: any }
? TypePropertyIsNotAllowed
: T;
export const arraysAreNotAllowedMsg =
'arrays are not allowed in action creators';
type ArraysAreNotAllowed = typeof arraysAreNotAllowedMsg;

export type DisallowArraysAndTypeProperty<T> = T extends any[]
? ArraysAreNotAllowed
: T extends { type: any } ? TypePropertyIsNotAllowed : T;

export const typePropertyIsNotAllowedMsg =
'type property is not allowed in action creators';
Expand All @@ -67,13 +71,17 @@ type TypePropertyIsNotAllowed = typeof typePropertyIsNotAllowedMsg;
export type Creator<
P extends any[] = any[],
R extends object = object
> = R extends { type: any }
? TypePropertyIsNotAllowed
: FunctionWithParametersType<P, R>;

export type PropsReturnType<T extends object> = T extends { type: any }
? TypePropertyIsNotAllowed
: { _as: 'props'; _p: T };
> = R extends any[]
? ArraysAreNotAllowed
: R extends { type: any }
? TypePropertyIsNotAllowed
: FunctionWithParametersType<P, R>;

export type PropsReturnType<T extends object> = T extends any[]
? ArraysAreNotAllowed
: T extends { type: any }
? TypePropertyIsNotAllowed
: { _as: 'props'; _p: T };

/**
* See `Creator`.
Expand Down

0 comments on commit 1e4c0be

Please sign in to comment.