Skip to content

Commit

Permalink
Merge pull request #7012 from matt-tingen/actions-type
Browse files Browse the repository at this point in the history
Improve actions type
  • Loading branch information
gaetanmaisse authored Jul 2, 2019
2 parents 83b326d + bdb8f74 commit 4922763
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 6 deletions.
81 changes: 81 additions & 0 deletions addons/actions/src/models/ActionsFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { ActionOptions } from './ActionOptions';
import { ActionsMap } from './ActionsMap';

export interface ActionsFunction {
<T extends string>(handlerMap: Record<T, string>, options?: ActionOptions): ActionsMap<T>;
<T extends string>(...handlers: T[]): ActionsMap<T>;

<T extends string>(handler1: T, options?: ActionOptions): ActionsMap<T>;
<T extends string>(handler1: T, handler2: T, options?: ActionOptions): ActionsMap<T>;
<T extends string>(handler1: T, handler2: T, handler3: T, options?: ActionOptions): ActionsMap<T>;
<T extends string>(
handler1: T,
handler2: T,
handler3: T,
handler4: T,
options?: ActionOptions
): ActionsMap<T>;
<T extends string>(
handler1: T,
handler2: T,
handler3: T,
handler4: T,
handler5: T,
options?: ActionOptions
): ActionsMap<T>;
<T extends string>(
handler1: T,
handler2: T,
handler3: T,
handler4: T,
handler5: T,
handler6: T,
options?: ActionOptions
): ActionsMap<T>;
<T extends string>(
handler1: T,
handler2: T,
handler3: T,
handler4: T,
handler5: T,
handler6: T,
handler7: T,
options?: ActionOptions
): ActionsMap<T>;
<T extends string>(
handler1: T,
handler2: T,
handler3: T,
handler4: T,
handler5: T,
handler6: T,
handler7: T,
handler8: T,
options?: ActionOptions
): ActionsMap<T>;
<T extends string>(
handler1: T,
handler2: T,
handler3: T,
handler4: T,
handler5: T,
handler6: T,
handler7: T,
handler8: T,
handler9: T,
options?: ActionOptions
): ActionsMap<T>;
<T extends string>(
handler1: T,
handler2: T,
handler3: T,
handler4: T,
handler5: T,
handler6: T,
handler7: T,
handler8: T,
handler9: T,
handler10: T,
options?: ActionOptions
): ActionsMap<T>;
}
4 changes: 1 addition & 3 deletions addons/actions/src/models/ActionsMap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { HandlerFunction } from './HandlerFunction';

export interface ActionsMap {
[key: string]: HandlerFunction;
}
export type ActionsMap<T extends string = string> = Record<T, HandlerFunction>;
1 change: 1 addition & 0 deletions addons/actions/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './ActionDisplay';
export * from './ActionsFunction';
export * from './ActionOptions';
export * from './ActionsMap';
export * from './DecoratorFunction';
Expand Down
6 changes: 3 additions & 3 deletions addons/actions/src/preview/actions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { action } from './action';
import { ActionOptions, ActionsMap } from '../models';
import { ActionsFunction, ActionOptions, ActionsMap } from '../models';
import { config } from './configureActions';

export function actions(...args: any[]): ActionsMap {
export const actions: ActionsFunction = (...args: any[]) => {
let options: ActionOptions = config;
const names = args;
// last argument can be options
Expand All @@ -26,4 +26,4 @@ export function actions(...args: any[]): ActionsMap {
actionsObject[name] = action(namesObject[name], options);
});
return actionsObject;
}
};

1 comment on commit 4922763

@vercel
Copy link

@vercel vercel bot commented on 4922763 Jul 2, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.