Skip to content

Commit

Permalink
Merge pull request #16 from byte-fe/develop
Browse files Browse the repository at this point in the history
refactor(TS): Improve the readability of type definition
  • Loading branch information
ArrayZoneYour authored Dec 29, 2018
2 parents a64fa1e + b866807 commit d493d47
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,20 @@ TypeScript Example
// StateType and ActionsParamType definition
// ...

const Model = {
const Model: ModelType<StateType, ActionsParamType> = {
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<StateType, ActionsParamType>
}
```

JavaScript Example
Expand Down
8 changes: 4 additions & 4 deletions example/model/home.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ type ActionsParamType = {
get: undefined
}

const Model = {
const Model: ModelType<StateType, ActionsParamType> = {
actions: {
increment: async (s, __, params) => {
return (state: typeof s) => {
increment: async (_, __, params) => {
return (state: typeof _) => {
state.counter += params || 1
}
},
Expand Down Expand Up @@ -50,7 +50,7 @@ const Model = {
}
},
state: initialState
} as ModelType<StateType, ActionsParamType>
}

export default Model

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -16,7 +16,7 @@
],
"author": "ArrayZoneYour <[email protected]>",
"license": "MIT",
"dependencies": {
"peerDependencies": {
"immer": "^1.9.3",
"react": "^16.7.0-alpha.2",
"react-dom": "^16.7.0-alpha.2"
Expand Down
27 changes: 13 additions & 14 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
type State<T> = T

type Action<T, P = any, ActionKeys = []> = (
state: T,
actions: getConsumerActionsType<Actions<T, ActionKeys>>,
// Very Sad, Promise<ProduceFunc<S>> can not work with Partial<S> | ProduceFunc<S>
type Action<S = {}, P = any, ActionKeys = {}> = (
state: S,
actions: getConsumerActionsType<Actions<S, ActionKeys>>,
params: P
) => Partial<T> | ProduceFunc<T>
) => Partial<S> | Promise<Partial<S>> | ProduceFunc<S> | Promise<ProduceFunc<S>>

type ProduceFunc = <T>(state?: T) => {}
type ProduceFunc<S> = (state?: S) => {}

type ProviderProps = { [name: string]: ModelType }

type Actions<T, ActionKeys> = {
[P in keyof ActionKeys]: Action<T, ActionKeys[P], ActionKeys>
type Actions<S = {}, ActionKeys = {}> = {
[P in keyof ActionKeys]: Action<S, ActionKeys[P], ActionKeys>
}

interface Models {
Expand All @@ -29,10 +28,10 @@ type ArgumentTypes<F extends Function> = F extends (...args: infer A) => any
? A
: never

type getConsumerActionsType<T> = {
[P in keyof T]: ArgumentTypes<T[P]>[2] extends undefined
? (params?: ArgumentTypes<T[P]>[2]) => ReturnType<T[P]>
: (params: ArgumentTypes<T[P]>[2]) => ReturnType<T[P]>
type getConsumerActionsType<A extends Actions> = {
[P in keyof A]: ArgumentTypes<A[P]>[2] extends undefined
? <K extends keyof A>(params?: ArgumentTypes<A[K]>[2]) => ReturnType<A[K]>
: <K extends keyof A>(params: ArgumentTypes<A[K]>[2]) => ReturnType<A[K]>
}

type Get<T, N extends keyof T> = T[N]
type Get<Object, K extends keyof Object> = Object[K]

0 comments on commit d493d47

Please sign in to comment.