Skip to content

Commit 049bc62

Browse files
authored
Merge pull request #27 from stkevintan/feat_type
feat: remove other props in actions type inference
2 parents b7b644b + 3e3b0f6 commit 049bc62

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

demo/index.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Count extends Ayanami<State> {
3232
count: 0,
3333
}
3434

35+
otherProps: string = ''
36+
3537
constructor(private readonly tips: Tips) {
3638
super()
3739
}
@@ -41,6 +43,11 @@ class Count extends Ayanami<State> {
4143
return { count: state.count + count }
4244
}
4345

46+
@Reducer()
47+
addOne(state: State): State {
48+
return { count: state.count + 1 }
49+
}
50+
4451
@Reducer()
4552
reset(): State {
4653
return { count: 0 }

src/core/types.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,29 @@ type UnpackPayload<F, S> = UnpackEffectPayload<F, S> extends never
9494
: UnpackReducerPayload<F, S>
9595
: UnpackEffectPayload<F, S>
9696

97+
type PayloadMethodKeySet<M, S, SS extends keyof M> = {
98+
[key in SS]: M[key] extends
99+
| (() => Observable<EffectAction>)
100+
| ((payload$: Observable<any>) => Observable<EffectAction>)
101+
| ((payload$: Observable<any>, state$: Observable<S>) => Observable<EffectAction>)
102+
? key
103+
: M[key] extends (() => S) | ((state: S) => S) | ((state: S, payload: any) => S)
104+
? key
105+
: M[key] extends ((state: Draft<S>) => void) | ((state: Draft<S>, payload: any) => void)
106+
? key
107+
: M[key] extends Observable<any>
108+
? key
109+
: never
110+
}[SS]
111+
97112
export type ActionMethodOfAyanami<M extends Ayanami<S>, S> = Pick<
98-
{
99-
[key in keyof M]: UnpackPayload<M[key], S> extends never
100-
? never
101-
: ActionMethod<UnpackPayload<M[key], S>>
102-
},
103-
Exclude<keyof M, keyof Ayanami<S>>
113+
{ [key in keyof M]: ActionMethod<UnpackPayload<M[key], S>> },
114+
PayloadMethodKeySet<M, S, Exclude<keyof M, keyof Ayanami<S>>>
104115
>
105116

106117
export type ActionOfAyanami<M extends Ayanami<S>, S> = Pick<
107-
{
108-
[key in keyof M]: UnpackPayload<M[key], S> extends never
109-
? never
110-
: ActionMethod<UnpackPayload<M[key], S>, EffectAction>
111-
},
112-
Exclude<keyof M, keyof Ayanami<S>>
118+
{ [key in keyof M]: ActionMethod<UnpackPayload<M[key], S>, EffectAction> },
119+
PayloadMethodKeySet<M, S, Exclude<keyof M, keyof Ayanami<S>>>
113120
>
114121

115122
export interface ObjectOf<T> {

0 commit comments

Comments
 (0)