Skip to content

Commit 1e4c0be

Browse files
chrisguttandinbrandonroberts
authored andcommitted
fix(store): disallow arrays in action creators (#2155)
1 parent 881c6bd commit 1e4c0be

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

modules/store/spec/action_creator.spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,20 @@ describe('Action Creators', () => {
7777
const value = fooAction.bar;
7878
`).toFail(/'bar' does not exist on type/);
7979
});
80+
8081
it('should not allow type property', () => {
8182
expectSnippet(`
8283
const foo = createAction('FOO', (type: string) => ({type}));
8384
`).toFail(
84-
/Type '{ type: string; }' is not assignable to type '"type property is not allowed in action creators"/
85+
/Type '{ type: string; }' is not assignable to type '"type property is not allowed in action creators"'/
86+
);
87+
});
88+
89+
it('should not allow ararys', () => {
90+
expectSnippet(`
91+
const foo = createAction('FOO', () => [ ]);
92+
`).toFail(
93+
/Type 'any\[]' is not assignable to type '"arrays are not allowed in action creators"'/
8594
);
8695
});
8796
});
@@ -150,12 +159,19 @@ describe('Action Creators', () => {
150159
});
151160

152161
it('should not allow type property', () => {
153-
const foo = createAction('FOO', props<{ type: number }>() as any);
154162
expectSnippet(`
155163
const foo = createAction('FOO', props<{ type: number }>());
156164
`).toFail(
157165
/Argument of type '"type property is not allowed in action creators"' is not assignable to parameter of type/
158166
);
159167
});
168+
169+
it('should not allow ararys', () => {
170+
expectSnippet(`
171+
const foo = createAction('FOO', props<[]>());
172+
`).toFail(
173+
/Argument of type '"arrays are not allowed in action creators"' is not assignable to parameter of type/
174+
);
175+
});
160176
});
161177
});

modules/store/src/action_creator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
TypedAction,
55
FunctionWithParametersType,
66
PropsReturnType,
7-
DisallowTypeProperty,
7+
DisallowArraysAndTypeProperty,
88
} from './models';
99

1010
// Action creators taken from ts-action library and modified a bit to better
@@ -23,7 +23,7 @@ export function createAction<
2323
R extends object
2424
>(
2525
type: T,
26-
creator: Creator<P, DisallowTypeProperty<R>>
26+
creator: Creator<P, DisallowArraysAndTypeProperty<R>>
2727
): FunctionWithParametersType<P, R & TypedAction<T>> & TypedAction<T>;
2828
/**
2929
* @description

modules/store/src/models.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ export type SelectorWithProps<State, Props, Result> = (
5353
props: Props
5454
) => Result;
5555

56-
export type DisallowTypeProperty<T> = T extends { type: any }
57-
? TypePropertyIsNotAllowed
58-
: T;
56+
export const arraysAreNotAllowedMsg =
57+
'arrays are not allowed in action creators';
58+
type ArraysAreNotAllowed = typeof arraysAreNotAllowedMsg;
59+
60+
export type DisallowArraysAndTypeProperty<T> = T extends any[]
61+
? ArraysAreNotAllowed
62+
: T extends { type: any } ? TypePropertyIsNotAllowed : T;
5963

6064
export const typePropertyIsNotAllowedMsg =
6165
'type property is not allowed in action creators';
@@ -67,13 +71,17 @@ type TypePropertyIsNotAllowed = typeof typePropertyIsNotAllowedMsg;
6771
export type Creator<
6872
P extends any[] = any[],
6973
R extends object = object
70-
> = R extends { type: any }
71-
? TypePropertyIsNotAllowed
72-
: FunctionWithParametersType<P, R>;
73-
74-
export type PropsReturnType<T extends object> = T extends { type: any }
75-
? TypePropertyIsNotAllowed
76-
: { _as: 'props'; _p: T };
74+
> = R extends any[]
75+
? ArraysAreNotAllowed
76+
: R extends { type: any }
77+
? TypePropertyIsNotAllowed
78+
: FunctionWithParametersType<P, R>;
79+
80+
export type PropsReturnType<T extends object> = T extends any[]
81+
? ArraysAreNotAllowed
82+
: T extends { type: any }
83+
? TypePropertyIsNotAllowed
84+
: { _as: 'props'; _p: T };
7785

7886
/**
7987
* See `Creator`.

0 commit comments

Comments
 (0)