Skip to content

Commit

Permalink
Mapped type for combineReducers in index.d.ts (#2182)
Browse files Browse the repository at this point in the history
* Add mapped type for combineReducers in index.d.ts

Updated typescript to 2.1.4
Updated typescript-definition-tester to 0.0.5
Updated typescript tests to use proper import
Added mapped type to index.d.ts

* add strict null check for reducer

Updated Reducer<S> type in index.d.ts
Add strictNullChecks flag to typescript spec
  • Loading branch information
mkusher authored and timdorr committed Apr 25, 2017
1 parent 5c60ef7 commit 8033325
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 28 deletions.
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export interface Action {
*
* @template S State object type.
*/
export type Reducer<S> = <A extends Action>(state: S, action: A) => S;
export type Reducer<S> = <A extends Action>(state: S | undefined, action: A) => S;

/**
* Object whose values correspond to different reducer functions.
*/
export interface ReducersMapObject {
[key: string]: Reducer<any>;
export type ReducersMapObject<S> = {
[K in keyof S]: Reducer<S[K]>;
}

/**
Expand All @@ -70,7 +70,7 @@ export interface ReducersMapObject {
* @returns A reducer function that invokes every reducer inside the passed
* object, and builds a state object with the same shape.
*/
export function combineReducers<S>(reducers: ReducersMapObject): Reducer<S>;
export function combineReducers<S>(reducers: ReducersMapObject<S>): Reducer<S>;


/* store */
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
"rollup-plugin-replace": "^1.1.1",
"rollup-plugin-uglify": "^1.0.1",
"rxjs": "^5.0.0-beta.6",
"typescript": "^1.8.0",
"typescript-definition-tester": "0.0.4"
"typescript": "^2.1.0",
"typescript-definition-tester": "0.0.5"
},
"npmName": "redux",
"npmFileMap": [
Expand Down
3 changes: 3 additions & 0 deletions test/typescript.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ describe('TypeScript definitions', function () {
tt.compileDirectory(
__dirname + '/typescript',
fileName => fileName.match(/\.ts$/),
{
strictNullChecks: true
},
() => done()
)
})
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/actionCreators.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ActionCreator, Action, Dispatch,
bindActionCreators, ActionCreatorsMapObject
} from "../../index";
} from "../../"


interface AddTodoAction extends Action {
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Action as ReduxAction} from "../../index";
import {Action as ReduxAction} from "../../"


namespace FSA {
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/compose.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {compose} from "../../index";
import {compose} from "../../"

// copied from DefinitelyTyped/compose-function

Expand Down
4 changes: 2 additions & 2 deletions test/typescript/dispatch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {Dispatch, Action} from "../../index";
import {Dispatch, Action} from "../../"


declare const dispatch: Dispatch<any>;

const dispatchResult: Action = dispatch({type: 'TYPE'});

// thunk
declare module "../../index" {
declare module "../../" {
export interface Dispatch<S> {
<R>(asyncAction: (dispatch: Dispatch<S>, getState: () => S) => R): R;
}
Expand Down
8 changes: 4 additions & 4 deletions test/typescript/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {
Middleware, MiddlewareAPI,
applyMiddleware, createStore, Dispatch, Reducer, Action
} from "../../index";
} from "../../"

declare module "../../index" {
declare module "../../" {
export interface Dispatch<S> {
<R>(asyncAction: (dispatch: Dispatch<S>, getState: () => S) => R): R;
}
}

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

const thunkMiddleware: Middleware =
<S>({dispatch, getState}: MiddlewareAPI<S>) =>
Expand Down Expand Up @@ -51,7 +51,7 @@ const storeWithThunkMiddleware = createStore(
);

storeWithThunkMiddleware.dispatch(
(dispatch, getState) => {
(dispatch: Dispatch<State>, getState: () => State) => {
const todos: string[] = getState().todos;
dispatch({type: 'ADD_TODO'})
}
Expand Down
5 changes: 2 additions & 3 deletions test/typescript/reducers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Reducer, Action, combineReducers,
ReducersMapObject
} from "../../index";
} from "../../"


type TodosState = string[];
Expand Down Expand Up @@ -47,8 +47,7 @@ type RootState = {
counter: CounterState;
}


const rootReducer: Reducer<RootState> = combineReducers<RootState>({
const rootReducer: Reducer<RootState> = combineReducers({
todos: todosReducer,
counter: counterReducer,
})
Expand Down
6 changes: 3 additions & 3 deletions test/typescript/store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Store, createStore, Reducer, Action, StoreEnhancer, GenericStoreEnhancer,
StoreCreator, StoreEnhancerStoreCreator, Unsubscribe
} from "../../index";
} from "../../"


type State = {
Expand All @@ -22,10 +22,10 @@ const storeWithPreloadedState: Store<State> = createStore(reducer, {
});

const genericEnhancer: GenericStoreEnhancer = <S>(next: StoreEnhancerStoreCreator<S>) => next;
const specificEnhencer: StoreEnhancer<State> = next => next;
const specificEnhancer: StoreEnhancer<State> = next => next;

const storeWithGenericEnhancer: Store<State> = createStore(reducer, genericEnhancer);
const storeWithSpecificEnhancer: Store<State> = createStore(reducer, specificEnhencer);
const storeWithSpecificEnhancer: Store<State> = createStore(reducer, specificEnhancer);

const storeWithPreloadedStateAndEnhancer: Store<State> = createStore(reducer, {
todos: []
Expand Down
18 changes: 11 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2610,7 +2610,11 @@ lodash.flatten@^4.4.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/lodash.flatten/-/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"

lodash.isarguments@^3.0.0, lodash.isarguments@~3.0.7:
lodash.isarguments@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a"

lodash.isarguments@~3.0.7:
version "3.0.9"
resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.0.9.tgz#3c4994a4210f340d49ccfafa62176296207d8675"

Expand Down Expand Up @@ -4143,17 +4147,17 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

typescript-definition-tester@0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/typescript-definition-tester/-/typescript-definition-tester-0.0.4.tgz#94b9edc4fe803b47f5f64ff5ddaf8eed1196156c"
typescript-definition-tester@0.0.5:
version "0.0.5"
resolved "https://registry.yarnpkg.com/typescript-definition-tester/-/typescript-definition-tester-0.0.5.tgz#91c574d78ea05b81ed81244d50ec30d8240c356f"
dependencies:
assertion-error "^1.0.1"
dts-bundle "^0.2.0"
lodash "^3.6.0"

typescript@^1.8.0:
version "1.8.10"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-1.8.10.tgz#b475d6e0dff0bf50f296e5ca6ef9fbb5c7320f1e"
typescript@^2.1.0:
version "2.1.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.1.4.tgz#b53b69fb841126acb1dd4b397d21daba87572251"

uglify-js@^2.6, uglify-js@^2.6.1:
version "2.7.5"
Expand Down

0 comments on commit 8033325

Please sign in to comment.