@@ -47,12 +47,32 @@ type Action<S = {}, P = any, ActionKeys = {}> = (
4747 | void
4848 | Promise < void >
4949
50+ // v3.0 Action
51+ type NextAction < S = { } , P = any , ActionKeys = { } > = (
52+ params : P ,
53+ context : {
54+ state : S
55+ actions : getConsumerNextActionsType < NextActions < S , ActionKeys > >
56+ }
57+ ) =>
58+ | Partial < S >
59+ | Promise < Partial < S > >
60+ | ProduceFunc < S >
61+ | Promise < ProduceFunc < S > >
62+ | void
63+ | Promise < void >
64+
5065type ProduceFunc < S > = ( state : S ) => void
5166
5267type Actions < S = { } , ActionKeys = { } > = {
5368 [ P in keyof ActionKeys ] : Action < S , ActionKeys [ P ] , ActionKeys >
5469}
5570
71+ // v3.0 Actions
72+ type NextActions < S = { } , ActionKeys = { } > = {
73+ [ P in keyof ActionKeys ] : NextAction < S , ActionKeys [ P ] , ActionKeys >
74+ }
75+
5676type Dispatch < A > = ( value : A ) => void
5777type SetStateAction < S > = S | ( ( prevState : S ) => S )
5878
@@ -90,14 +110,14 @@ type Middleware<S = {}> = (C: Context<S>, M: Middleware<S>[]) => void
90110interface Models < State = any , ActionKeys = any > {
91111 [ name : string ] :
92112 | ModelType < State , ActionKeys >
93- | API < ModelType < State , ActionKeys > >
113+ | API < NextModelType < State , ActionKeys > >
94114}
95115
96- interface API < MT extends ModelType = any > {
116+ interface API < MT extends NextModelType = any > {
97117 __id : string
98118 useStore : (
99119 depActions ?: Array < keyof MT [ 'actions' ] >
100- ) => [ Get < MT , 'state' > , getConsumerActionsType < Get < MT , 'actions' > > ]
120+ ) => [ Get < MT , 'state' > , getConsumerNextActionsType < Get < MT , 'actions' > > ]
101121 getState : ( ) => Readonly < Get < MT , 'state' > >
102122 subscribe : (
103123 actionName : keyof MT [ 'actions' ] | Array < keyof MT [ 'actions' ] > ,
@@ -106,7 +126,7 @@ interface API<MT extends ModelType = any> {
106126 unsubscribe : (
107127 actionName : keyof Get < MT , 'actions' > | Array < keyof Get < MT , 'actions' > >
108128 ) => void
109- actions : Readonly < getConsumerActionsType < Get < MT , 'actions' > > >
129+ actions : Readonly < getConsumerNextActionsType < Get < MT , 'actions' > > >
110130}
111131
112132interface APIs < M extends Models > {
@@ -158,6 +178,19 @@ type ModelType<InitStateType = any, ActionKeys = any> = {
158178 asyncState ?: ( context ?: any ) => Promise < Partial < InitStateType > >
159179}
160180
181+ // v3.0
182+ type NextModelType < InitStateType = any , ActionKeys = any > = {
183+ actions : {
184+ [ P in keyof ActionKeys ] : NextAction <
185+ InitStateType ,
186+ ActionKeys [ P ] ,
187+ ActionKeys
188+ >
189+ }
190+ state : InitStateType
191+ asyncState ?: ( context ?: any ) => Promise < Partial < InitStateType > >
192+ }
193+
161194type ArgumentTypes < F extends Function > = F extends ( ...args : infer A ) => any
162195 ? A
163196 : never
@@ -174,6 +207,13 @@ type getConsumerActionsType<A extends Actions<any, any>> = {
174207 ) => ReturnType < A [ P ] >
175208}
176209
210+ // v3.0
211+ type getConsumerNextActionsType < A extends NextActions < any , any > > = {
212+ [ P in keyof A ] : ArgumentTypes < A [ P ] > [ 0 ] extends undefined
213+ ? ( params ?: ArgumentTypes < A [ P ] > [ 0 ] ) => ReturnType < A [ P ] >
214+ : ( params : ArgumentTypes < A [ P ] > [ 0 ] ) => ReturnType < A [ P ] >
215+ }
216+
177217type Get < Object , K extends keyof Object > = Object [ K ]
178218
179219type ModelsProps < M extends Models > = {
0 commit comments