Skip to content

Commit 00cc012

Browse files
authored
Merge pull request #77 from aikoven/master
Add TypeScript definitions
2 parents 9095294 + 2294698 commit 00cc012

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {Middleware, Dispatch} from "redux";
2+
3+
4+
export type ThunkAction<R, S, E> = (dispatch: Dispatch<S>, getState: () => S,
5+
extraArgument: E) => R;
6+
7+
declare module "redux" {
8+
export interface Dispatch<S> {
9+
<R, E>(asyncAction: ThunkAction<R, S, E>): R;
10+
}
11+
}
12+
13+
14+
declare const thunk: Middleware & {
15+
withExtraArgument(extraArgument: any): Middleware;
16+
};
17+
18+
export default thunk;

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
"description": "Thunk middleware for Redux.",
55
"main": "lib/index.js",
66
"jsnext:main": "es/index.js",
7+
"typings": "./index.d.ts",
78
"files": [
89
"lib",
910
"es",
1011
"src",
11-
"dist"
12+
"dist",
13+
"index.d.ts"
1214
],
1315
"scripts": {
1416
"clean": "rimraf lib dist es",
@@ -67,7 +69,10 @@
6769
"eslint-config-airbnb": "1.0.2",
6870
"eslint-plugin-react": "^4.1.0",
6971
"mocha": "^2.2.5",
72+
"redux": "^3.4.0",
7073
"rimraf": "^2.5.2",
74+
"typescript": "^1.8.10",
75+
"typescript-definition-tester": "0.0.4",
7176
"webpack": "^1.12.14"
7277
}
7378
}

test/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import chai from 'chai';
22
import thunkMiddleware from '../src/index';
3+
import * as tt from 'typescript-definition-tester';
4+
35

46
describe('thunk middleware', () => {
57
const doDispatch = () => {};
@@ -91,4 +93,16 @@ describe('thunk middleware', () => {
9193
});
9294
});
9395
});
96+
97+
describe('TypeScript definitions', function test() {
98+
this.timeout(0);
99+
100+
it('should compile against index.d.ts', (done) => {
101+
tt.compileDirectory(
102+
__dirname,
103+
fileName => fileName.match(/\.ts$/),
104+
() => done()
105+
);
106+
});
107+
});
94108
});

test/typescript.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {Store, Middleware} from 'redux';
2+
import thunk, {ThunkAction} from '../index.d.ts';
3+
4+
5+
declare const store: Store<{foo: string}>;
6+
7+
store.dispatch(dispatch => {
8+
dispatch({type: 'FOO'});
9+
});
10+
11+
store.dispatch((dispatch, getState) => {
12+
const state = getState();
13+
14+
const foo: string = state.foo;
15+
});
16+
17+
const middleware: Middleware = thunk.withExtraArgument('bar');
18+
19+
store.dispatch((dispatch, getState, extraArg) => {
20+
console.log(extraArg);
21+
});
22+
23+
const thunkAction: ThunkAction<void, {foo: string}, {bar: number}> =
24+
(dispatch, getState, extraArg) => {
25+
const foo: string = getState().foo;
26+
const bar: number = extraArg.bar;
27+
28+
dispatch({type: 'FOO'});
29+
};
30+
31+
const thunkActionDispatchOnly: ThunkAction<void, {}, {}> = dispatch => {
32+
dispatch({type: 'FOO'});
33+
};

0 commit comments

Comments
 (0)