Skip to content

Commit a5d44fa

Browse files
committed
Update TypeScript definitions
Remove redundant type parameters from `Dispatch` type Remove redundant `combineReducers` overload Simplify `StoreCreator` type by using optional arguments Remove redundant type parameters from `Middleware` type Update `compose` definitions to cover case with zero arguments and case with multiple arguments for innermost function Add JSDoc
1 parent 6317727 commit a5d44fa

File tree

8 files changed

+360
-85
lines changed

8 files changed

+360
-85
lines changed

index.d.ts

Lines changed: 322 additions & 43 deletions
Large diffs are not rendered by default.

test/typescript/actionCreators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from "../../index.d.ts";
55

66

7-
interface AddTodoAction extends Action<string> {
7+
interface AddTodoAction extends Action {
88
text: string;
99
}
1010

test/typescript/actions.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Action as ReduxAction} from "../../index.d.ts";
22

33

44
namespace FSA {
5-
interface Action<P> extends ReduxAction<string> {
5+
interface Action<P> extends ReduxAction {
66
payload: P;
77
}
88

@@ -16,7 +16,7 @@ namespace FSA {
1616

1717

1818
namespace FreeShapeAction {
19-
interface Action extends ReduxAction<string> {
19+
interface Action extends ReduxAction {
2020
[key: string]: any;
2121
}
2222

@@ -32,7 +32,11 @@ namespace FreeShapeAction {
3232
namespace StringLiteralTypeAction {
3333
type ActionType = 'A' | 'B' | 'C';
3434

35-
const action: ReduxAction<ActionType> = {
35+
interface Action extends ReduxAction {
36+
type: ActionType;
37+
}
38+
39+
const action: Action = {
3640
type: 'A'
3741
}
3842

@@ -45,7 +49,11 @@ namespace EnumTypeAction {
4549
A, B, C
4650
}
4751

48-
const action: ReduxAction<ActionType> = {
52+
interface Action extends ReduxAction {
53+
type: ActionType;
54+
}
55+
56+
const action: Action = {
4957
type: ActionType.A
5058
}
5159

test/typescript/compose.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ const t4: (a: string) => number = compose(
1818
const t5: number = compose(stringToNumber, numberToString, numberToNumber)(5);
1919
const t6: string = compose(numberToString, stringToNumber, numberToString, numberToNumber)(5);
2020

21-
const t7: string = compose<string, string>(
21+
const t7: string = compose<string>(
2222
numberToString, numberToNumber, stringToNumber, numberToString, stringToNumber)("fo");

test/typescript/dispatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {Dispatch, Action} from "../../index.d.ts";
44
declare const dispatch: Dispatch;
55

66

7-
const dispatchResult: Action<string> = dispatch({type: 'TYPE'});
7+
const dispatchResult: Action = dispatch({type: 'TYPE'});
88

99

1010
type Thunk<O> = () => O;
1111

12-
const dispatchThunkResult: number = dispatch<Thunk<number>, number>(() => 42);
12+
const dispatchThunkResult: number = dispatch(() => 42);

test/typescript/middleware.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,38 @@ import {
66

77
type Thunk<S, O> = (dispatch: Dispatch, getState?: () => S) => O;
88

9-
109
const thunkMiddleware: Middleware =
1110
<S>({dispatch, getState}: MiddlewareAPI<S>) =>
1211
(next: Dispatch) =>
1312
<A, B>(action: A | Thunk<S, B>): B =>
1413
typeof action === 'function' ?
1514
(<Thunk<S, B>>action)(dispatch, getState) :
16-
next<A, B>(<A>action)
15+
<B>next(<A>action)
1716

1817

19-
const loggerMiddleware: Middleware = <S>({getState}: MiddlewareAPI<S>) =>
20-
(next: Dispatch) =>
21-
<A, B>(action: A): B => {
22-
console.log('will dispatch', action)
18+
const loggerMiddleware: Middleware =
19+
<S>({getState}: MiddlewareAPI<S>) =>
20+
(next: Dispatch) =>
21+
(action: any): any => {
22+
console.log('will dispatch', action)
2323

24-
// Call the next dispatch method in the middleware chain.
25-
const returnValue: B = next<A, B>(action)
24+
// Call the next dispatch method in the middleware chain.
25+
const returnValue = next(action)
2626

27-
console.log('state after dispatch', getState())
27+
console.log('state after dispatch', getState())
2828

29-
// This will likely be the action itself, unless
30-
// a middleware further in chain changed it.
31-
return returnValue
32-
}
29+
// This will likely be the action itself, unless
30+
// a middleware further in chain changed it.
31+
return returnValue
32+
}
3333

3434

3535

3636
type State = {
3737
todos: string[];
3838
}
3939

40-
const reducer: Reducer<State> = (state: State, action: Action<any>): State => {
40+
const reducer: Reducer<State> = (state: State, action: Action): State => {
4141
return state;
4242
}
4343

@@ -46,7 +46,7 @@ const storeWithThunkMiddleware = createStore(
4646
applyMiddleware(thunkMiddleware)
4747
);
4848

49-
storeWithThunkMiddleware.dispatch<Thunk<State, void>, void>(
49+
storeWithThunkMiddleware.dispatch(
5050
(dispatch: Dispatch, getState: () => State) => {
5151
const todos: string[] = getState().todos;
5252
dispatch({type: 'ADD_TODO'})

test/typescript/reducers.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import {
66

77
type TodosState = string[];
88

9-
interface AddTodoAction extends Action<any> {
9+
interface AddTodoAction extends Action {
1010
text: string;
1111
}
1212

1313

1414
const todosReducer: Reducer<TodosState> = (state: TodosState,
15-
action: Action<any>): TodosState => {
15+
action: Action): TodosState => {
1616
switch (action.type) {
1717
case 'ADD_TODO':
1818
return [...state, (<AddTodoAction>action).text]
@@ -30,7 +30,9 @@ const todosState: TodosState = todosReducer([], {
3030
type CounterState = number;
3131

3232

33-
const counterReducer: Reducer<CounterState> = (state: CounterState, action: Action<any>): CounterState => {
33+
const counterReducer: Reducer<CounterState> = (
34+
state: CounterState, action: Action
35+
): CounterState => {
3436
switch (action.type) {
3537
case 'INCREMENT':
3638
return state + 1
@@ -55,16 +57,3 @@ const rootState: RootState = rootReducer(undefined, {
5557
type: 'ADD_TODO',
5658
text: 'test',
5759
})
58-
59-
60-
interface RootReducers extends ReducersMapObject {
61-
todos: Reducer<TodosState>;
62-
counter: Reducer<CounterState>;
63-
}
64-
65-
66-
const rootReducerStrict: Reducer<RootState> =
67-
combineReducers<RootState, RootReducers>({
68-
todos: todosReducer,
69-
counter: counterReducer,
70-
})

test/typescript/store.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type State = {
88
todos: string[];
99
}
1010

11-
const reducer: Reducer<State> = (state: State, action: Action<any>): State => {
11+
const reducer: Reducer<State> = (state: State, action: Action): State => {
1212
return state;
1313
}
1414

@@ -54,8 +54,7 @@ unsubscribe();
5454

5555
/* replaceReducer */
5656

57-
const newReducer: Reducer<State> = (state: State,
58-
action: Action<any>): State => {
57+
const newReducer: Reducer<State> = (state: State, action: Action): State => {
5958
return state;
6059
}
6160

0 commit comments

Comments
 (0)