Skip to content

Commit 21330f8

Browse files
committed
fix(hooks): useAyanami type define
1 parent b2402ff commit 21330f8

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/hooks/use-ayanami-instance.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import get from 'lodash/get'
44
import { ActionMethodOfAyanami, Ayanami, combineWithIkari } from '../core'
55
import { useSubscribeAyanamiState } from './use-subscribe-ayanami-state'
66

7-
export interface UseAyanamiInstanceConfig<S, U> {
7+
export interface UseAyanamiInstanceConfig<S = unknown, U = unknown> {
88
destroyWhenUnmount?: boolean
99
selector?: (state: S) => U
1010
}

src/hooks/use-ayanami.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,24 @@ interface Config<S, U> extends Partial<ScopeConfig> {
2323
selector?: (state: S) => U
2424
}
2525

26-
export function useAyanami<M extends Ayanami<S>, S, U = M extends Ayanami<infer SS> ? SS : S>(
26+
export function useAyanami<M extends Ayanami<any>, U = M extends Ayanami<infer S> ? S : never>(
2727
A: ConstructorOf<M>,
28-
config?: M extends Ayanami<infer SS> ? Config<SS, U> : Config<S, U>,
29-
): M extends Ayanami<infer SS> ? Result<M, SS, U> : Result<M, S, U> {
28+
config?: M extends Ayanami<infer S> ? Config<S, U> : never,
29+
): M extends Ayanami<infer S>
30+
? NonNullable<typeof config> extends Config<S, infer SS>
31+
? Result<M, S, SS>
32+
: Result<M, S, S>
33+
: never {
3034
const scope = get(config, 'scope')
3135
const selector = get(config, 'selector')
3236
const req = isSSREnabled() ? React.useContext(SSRContext) : null
3337
const reqScope = req ? createScopeWithRequest(req, scope) : scope
3438
const ayanami = React.useMemo(() => getInstanceWithScope(A, reqScope), [reqScope])
3539
ayanami.scopeName = scope || DEFAULT_SCOPE_NAME
3640

37-
const useAyanamiInstanceConfig = React.useMemo((): UseAyanamiInstanceConfig<S, U> => {
41+
const useAyanamiInstanceConfig = React.useMemo<UseAyanamiInstanceConfig>(() => {
3842
return { destroyWhenUnmount: scope === TransientScope, selector }
3943
}, [reqScope])
4044

41-
return useAyanamiInstance<M, S, U>(ayanami, useAyanamiInstanceConfig) as any
45+
return useAyanamiInstance(ayanami, useAyanamiInstanceConfig) as any
4246
}

test/specs/hooks.spec.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ describe('Hooks spec:', () => {
111111
})
112112

113113
const OuterComponent = () => {
114-
const [state, actions] = useAyanami(Count, { scope: TransientScope })
114+
const [{ count }, actions] = useAyanami(Count, { scope: TransientScope })
115115
const addOne = useCallback(() => actions.add(1), [])
116-
outerRenderSpy(state.count)
116+
outerRenderSpy(count)
117117
return (
118118
<div>
119119
<button onClick={addOne}>add one</button>

0 commit comments

Comments
 (0)