Skip to content

Commit

Permalink
better definition for mapDispatchToProps
Browse files Browse the repository at this point in the history
  • Loading branch information
cimdalli committed May 22, 2018
1 parent ecb89b2 commit 473600d
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 43 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "redux-ts",
"version": "4.1.2",
"description": "Utils to define redux reducer/action in typescript",
"main": "dist/redux-ts.min.js",
"main": "dist/redux-ts.production.min.js",
"typings": "dist/src/index.d.ts",
"files": [
"*.md",
Expand Down Expand Up @@ -72,6 +72,6 @@
"react": ">=15",
"react-dom": ">=15",
"react-redux": ">=5",
"redux": ">=3"
"redux": ">=4"
}
}
24 changes: 15 additions & 9 deletions src/helpers/redux.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActionCreatorDefinition, StateToProps, DispatchToProps } from '..'
import { ActionCreatorDefinition, DispatchToProps, Indexer } from '..'

export const createAction = <TPayload>(
type: string,
Expand All @@ -12,11 +12,17 @@ export const createAction = <TPayload>(
return creator
}

export const mapDispatchToProps: DispatchToProps = map => dispatch =>
Object.keys(map).reduce(
(prev, key) => ({
...prev,
[key]: (...params: any[]) => dispatch(map[key](...params)),
}),
{},
) as typeof map
export const mapDispatchToProps: DispatchToProps = map => (dispatch, own) => {
const mapper = <T extends Indexer>(m: T) =>
Object.keys(m).reduce(
(prev, key) => ({
...prev,
[key]: (...params: any[]) => dispatch(m[key](...params)),
}),
{},
) as typeof m

return typeof map === 'function' ? mapper(map(dispatch, own)) : mapper(map)

// : map => mapper(d, o)
}
23 changes: 11 additions & 12 deletions src/models/redux.model.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { Dispatch, AnyAction } from 'redux'
import { ActionCreatorDefinition } from '..'
import { AnyAction } from 'redux'
import { MapDispatchToPropsFunction, MapStateToProps } from 'react-redux'

export type Indexer<T = any> = { [key: string]: T }

export interface StateToProps<TState = any> {
<T extends { [key: string]: any } = {}, Town = {}>(
map: (store: TState, own: Town) => T,
): (store: TState, own: Town) => T
<T extends Indexer, TOwn>(
map: MapStateToProps<T, TOwn, TState>,
): MapStateToProps<T, TOwn, TState>
}

export interface DispatchToProps<
TDispatchAction extends AnyAction = AnyAction
> {
<T extends { [key: string]: (...params: any[]) => AnyAction }, Town>(
map: T,
own: Town,
): (dispatch: Dispatch<TDispatchAction>, own: Town) => T
export interface DispatchToProps {
<T extends Indexer<(...params: any[]) => AnyAction>, TOwn>(
map: T | MapDispatchToPropsFunction<T, TOwn>,
): MapDispatchToPropsFunction<T, TOwn>
}
27 changes: 14 additions & 13 deletions webpack.common.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
const webpack = require("webpack")
const nodeExternals = require("webpack-node-externals")
const webpack = require('webpack')
const nodeExternals = require('webpack-node-externals')

module.exports = {
externals: [nodeExternals()],
entry: "./src/index.ts",
entry: ['./src/index.ts'],
resolve: {
extensions: [".ts", ".tsx", ".js", ".jsx"]
extensions: ['.ts', '.tsx', '.js', '.jsx'],
},
module: {
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: "ts-loader",
loader: 'ts-loader',
options: {
compilerOptions: {
declaration: true,
sourceMap: true
}
}
}
]
}
]
sourceMap: true,
},
},
},
],
},
],
},

plugins: [
// new CopyWebpackPlugin(['./index.js']),
// new webpack.DefinePlugin({
// "process.env.NODE_ENV": JSON.stringify(env)
// }),
// new webpack.SourceMapDevToolPlugin({
// filename: "[file].map"
// })
]
],
}
12 changes: 6 additions & 6 deletions webpack.dev.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const merge = require("webpack-merge")
const common = require("./webpack.common.js")
const merge = require('webpack-merge')
const common = require('./webpack.common.js')

module.exports = merge(common, {
mode: "development",
devtool: "source-map",
mode: 'development',
devtool: 'source-map',
output: {
filename: "redux-ts.js"
}
filename: 'redux-ts.development.js',
},
})
2 changes: 1 addition & 1 deletion webpack.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ module.exports = merge(common, {
libraryTarget: 'umd',
umdNamedDefine: true,
globalObject: 'this',
filename: 'redux-ts.min.js',
filename: 'redux-ts.production.min.js',
},
})

0 comments on commit 473600d

Please sign in to comment.