Skip to content

Commit 34967bf

Browse files
committed
Update meta type to index signature
1 parent 5d37aa9 commit 34967bf

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/index.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ export interface AnyAction {
22
type: any;
33
}
44

5+
export type Meta = null | {[key: string]: any};
6+
57
export interface Action<P> extends AnyAction {
68
type: string;
79
payload: P;
810
error?: boolean;
9-
meta?: Object | null;
11+
meta?: Meta;
1012
}
1113

1214
export interface Success<P, S> {
@@ -28,11 +30,11 @@ export function isType<P>(
2830

2931
export interface ActionCreator<P> {
3032
type: string;
31-
(payload: P, meta?: Object | null): Action<P>;
33+
(payload: P, meta?: Meta): Action<P>;
3234
}
3335

3436
export interface EmptyActionCreator extends ActionCreator<undefined> {
35-
(payload?: undefined, meta?: Object | null): Action<undefined>;
37+
(payload?: undefined, meta?: Meta): Action<undefined>;
3638
}
3739

3840
export interface AsyncActionCreators<P, S, E> {
@@ -43,18 +45,18 @@ export interface AsyncActionCreators<P, S, E> {
4345
}
4446

4547
export interface ActionCreatorFactory {
46-
(type: string, commonMeta?: Object | null,
48+
(type: string, commonMeta?: Meta,
4749
error?: boolean): EmptyActionCreator;
48-
<P>(type: string, commonMeta?: Object | null,
50+
<P>(type: string, commonMeta?: Meta,
4951
isError?: boolean): ActionCreator<P>;
50-
<P>(type: string, commonMeta?: Object | null,
52+
<P>(type: string, commonMeta?: Meta,
5153
isError?: (payload: P) => boolean): ActionCreator<P>;
5254

5355
async<P, S>(
54-
type: string, commonMeta?: Object | null,
56+
type: string, commonMeta?: Meta,
5557
): AsyncActionCreators<P, S, any>;
5658
async<P, S, E>(
57-
type: string, commonMeta?: Object | null,
59+
type: string, commonMeta?: Meta,
5860
): AsyncActionCreators<P, S, E>;
5961
}
6062

@@ -74,7 +76,7 @@ export default function actionCreatorFactory(
7476
const base = prefix ? `${prefix}/` : "";
7577

7678
function actionCreator <P>(
77-
type: string, commonMeta?: Object | null,
79+
type: string, commonMeta?: Meta,
7880
isError: ((payload: P) => boolean) | boolean = defaultIsError,
7981
): ActionCreator<P> {
8082
const fullType = base + type;
@@ -87,7 +89,7 @@ export default function actionCreatorFactory(
8789
}
8890

8991
return Object.assign(
90-
(payload: P, meta?: Object | null) => {
92+
(payload: P, meta?: Meta) => {
9193
const action: Action<P> = {
9294
type: fullType,
9395
payload,
@@ -108,7 +110,7 @@ export default function actionCreatorFactory(
108110
}
109111

110112
function asyncActionCreators<P, S, E>(
111-
type: string, commonMeta?: Object | null,
113+
type: string, commonMeta?: Meta,
112114
): AsyncActionCreators<P, S, E> {
113115
return {
114116
type: base + type,

tests/typings/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,22 @@ function testIsType() {
8181
const foo: {} = action.payload;
8282
}
8383
}
84+
85+
function testMeta() {
86+
const a = actionCreator<{foo: string}>('');
87+
88+
a({foo: 'foo'});
89+
a({foo: 'foo'}, null);
90+
// typings:expect-error
91+
a({foo: 'foo'}, 1);
92+
// typings:expect-error
93+
a({foo: 'foo'}, 'foo');
94+
// typings:expect-error
95+
a({foo: 'foo'}, true);
96+
97+
const action = a({foo: 'foo'}, {bar: 'baz'});
98+
99+
// typings:expect-error
100+
action.meta.foo;
101+
action.meta!.foo;
102+
}

0 commit comments

Comments
 (0)