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): replace Creator with ActionCreator on createAction #2299

Merged
merged 2 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions modules/store/src/action_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
FunctionWithParametersType,
PropsReturnType,
DisallowArraysAndTypeProperty,
TypedCreator,
} from './models';

// Action creators taken from ts-action library and modified a bit to better
Expand Down Expand Up @@ -100,7 +101,7 @@ export function createAction<
export function createAction<T extends string, C extends Creator>(
type: T,
config?: { _as: 'props' } | C
): Creator {
): ActionCreator<T> {
meeroslav marked this conversation as resolved.
Show resolved Hide resolved
if (typeof config === 'function') {
return defineType(type, (...args: any[]) => ({
...config(...args),
Expand Down Expand Up @@ -133,7 +134,10 @@ export function union<
return undefined!;
}

function defineType(type: string, creator: Creator): Creator {
function defineType<T extends string>(
type: T,
creator: TypedCreator<T>
meeroslav marked this conversation as resolved.
Show resolved Hide resolved
): ActionCreator<T> {
return Object.defineProperty(creator, 'type', {
value: type,
writable: false,
Expand Down
8 changes: 7 additions & 1 deletion modules/store/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ export type Creator<
? TypePropertyIsNotAllowed
: FunctionWithParametersType<P, R>;

export type TypedCreator<
meeroslav marked this conversation as resolved.
Show resolved Hide resolved
T extends string,
P extends any[] = any[],
R extends object = object
> = FunctionWithParametersType<P, R & TypedAction<T>>;

export type PropsReturnType<T extends object> = T extends any[]
? ArraysAreNotAllowed
: T extends { type: any }
Expand All @@ -88,7 +94,7 @@ export type PropsReturnType<T extends object> = T extends any[]
*/
export type ActionCreator<
T extends string = string,
C extends Creator = Creator
C extends Creator = TypedCreator<T>
meeroslav marked this conversation as resolved.
Show resolved Hide resolved
> = C & TypedAction<T>;

export type FunctionWithParametersType<P extends unknown[], R = void> = (
Expand Down