diff --git a/README.md b/README.md index fd32473..3db2dcf 100644 --- a/README.md +++ b/README.md @@ -183,15 +183,20 @@ TypeScript Example // StateType and ActionsParamType definition // ... -const Model = { +const Model: ModelType = { actions: { increment: async (s, _, params) => { + // issue: https://github.com/Microsoft/TypeScript/issues/29196 + // async function return produce need define type manually. return (state: typeof s) => { state.counter += params || 1 } + }, + decrease: (s, _, params) => s => { + s.counter += params || 1 } } -} as ModelType +} ``` JavaScript Example diff --git a/example/model/home.model.ts b/example/model/home.model.ts index e066567..edbf91c 100644 --- a/example/model/home.model.ts +++ b/example/model/home.model.ts @@ -14,10 +14,10 @@ type ActionsParamType = { get: undefined } -const Model = { +const Model: ModelType = { actions: { - increment: async (s, __, params) => { - return (state: typeof s) => { + increment: async (_, __, params) => { + return (state: typeof _) => { state.counter += params || 1 } }, @@ -50,7 +50,7 @@ const Model = { } }, state: initialState -} as ModelType +} export default Model diff --git a/package.json b/package.json index 454db1c..7eff934 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-modelx", - "version": "1.0.5", + "version": "1.0.6", "description": "The State management library for React", "main": "./dist/index", "module": "./dist/index", @@ -16,7 +16,7 @@ ], "author": "ArrayZoneYour ", "license": "MIT", - "dependencies": { + "peerDependencies": { "immer": "^1.9.3", "react": "^16.7.0-alpha.2", "react-dom": "^16.7.0-alpha.2" diff --git a/src/index.d.ts b/src/index.d.ts index ebea893..7b8bb45 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,17 +1,16 @@ -type State = T - -type Action = ( - state: T, - actions: getConsumerActionsType>, +// Very Sad, Promise> can not work with Partial | ProduceFunc +type Action = ( + state: S, + actions: getConsumerActionsType>, params: P -) => Partial | ProduceFunc +) => Partial | Promise> | ProduceFunc | Promise> -type ProduceFunc = (state?: T) => {} +type ProduceFunc = (state?: S) => {} type ProviderProps = { [name: string]: ModelType } -type Actions = { - [P in keyof ActionKeys]: Action +type Actions = { + [P in keyof ActionKeys]: Action } interface Models { @@ -29,10 +28,10 @@ type ArgumentTypes = F extends (...args: infer A) => any ? A : never -type getConsumerActionsType = { - [P in keyof T]: ArgumentTypes[2] extends undefined - ? (params?: ArgumentTypes[2]) => ReturnType - : (params: ArgumentTypes[2]) => ReturnType +type getConsumerActionsType = { + [P in keyof A]: ArgumentTypes[2] extends undefined + ? (params?: ArgumentTypes[2]) => ReturnType + : (params: ArgumentTypes[2]) => ReturnType } -type Get = T[N] +type Get = Object[K]