diff --git a/modules/store/spec/action_creator.spec.ts b/modules/store/spec/action_creator.spec.ts index bf95e8087b..826ab35572 100644 --- a/modules/store/spec/action_creator.spec.ts +++ b/modules/store/spec/action_creator.spec.ts @@ -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"'/ ); }); }); @@ -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/ + ); + }); }); }); diff --git a/modules/store/src/action_creator.ts b/modules/store/src/action_creator.ts index e2cc6ecc78..3882eba322 100644 --- a/modules/store/src/action_creator.ts +++ b/modules/store/src/action_creator.ts @@ -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 @@ -23,7 +23,7 @@ export function createAction< R extends object >( type: T, - creator: Creator> + creator: Creator> ): FunctionWithParametersType> & TypedAction; /** * @description diff --git a/modules/store/src/models.ts b/modules/store/src/models.ts index 831d7a3ad9..52796ad834 100644 --- a/modules/store/src/models.ts +++ b/modules/store/src/models.ts @@ -53,9 +53,13 @@ export type SelectorWithProps = ( props: Props ) => Result; -export type DisallowTypeProperty = T extends { type: any } - ? TypePropertyIsNotAllowed - : T; +export const arraysAreNotAllowedMsg = + 'arrays are not allowed in action creators'; +type ArraysAreNotAllowed = typeof arraysAreNotAllowedMsg; + +export type DisallowArraysAndTypeProperty = T extends any[] + ? ArraysAreNotAllowed + : T extends { type: any } ? TypePropertyIsNotAllowed : T; export const typePropertyIsNotAllowedMsg = 'type property is not allowed in action creators'; @@ -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; - -export type PropsReturnType = T extends { type: any } - ? TypePropertyIsNotAllowed - : { _as: 'props'; _p: T }; +> = R extends any[] + ? ArraysAreNotAllowed + : R extends { type: any } + ? TypePropertyIsNotAllowed + : FunctionWithParametersType; + +export type PropsReturnType = T extends any[] + ? ArraysAreNotAllowed + : T extends { type: any } + ? TypePropertyIsNotAllowed + : { _as: 'props'; _p: T }; /** * See `Creator`.