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 disallow arrays in action creators #2155

Merged
merged 4 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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