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): prevent unexpected behavior of {} as a props type #2728

Merged
merged 2 commits into from
Oct 7, 2020
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
10 changes: 9 additions & 1 deletion modules/store/spec/types/action_creator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,21 @@ describe('createAction()', () => {
);
});

it('should not allow ararys', () => {
it('should not allow arrays', () => {
expectSnippet(`
const foo = createAction('FOO', props<[]>());
`).toFail(
/Type 'Props<\[\]>' is not assignable to type '"arrays are not allowed in action creators"'/
);
});

it('should not allow empty objects', () => {
expectSnippet(`
const foo = createAction('FOO', props<{}>());
`).toFail(
/Type 'Props<\{\}>' is not assignable to type '"empty objects are not allowed in action creators"'/
);
});
});

describe('with function', () => {
Expand Down
6 changes: 6 additions & 0 deletions modules/store/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export const typePropertyIsNotAllowedMsg =
'type property is not allowed in action creators';
type TypePropertyIsNotAllowed = typeof typePropertyIsNotAllowedMsg;

export const emptyObjectsAreNotAllowedMsg =
'empty objects are not allowed in action creators';
type EmptyObjectsAreNotAllowed = typeof emptyObjectsAreNotAllowedMsg;

export type FunctionIsNotAllowed<
T,
ErrorMessage extends string
Expand All @@ -77,6 +81,8 @@ export type NotAllowedCheck<T extends object> = T extends any[]
? ArraysAreNotAllowed
: T extends { type: any }
? TypePropertyIsNotAllowed
: keyof T extends never
? EmptyObjectsAreNotAllowed
: unknown;

/**
Expand Down