Skip to content

Commit 611be45

Browse files
committed
typings(use-event-callback): imporve eventcallback signature
1 parent 5acc011 commit 611be45

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/use-event-callback.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import { Observable, BehaviorSubject, Subject, noop } from 'rxjs'
33

44
import { RestrictArray, VoidAsNull, Not } from './type'
55

6+
type VoidableCallback<EventValue> = EventValue extends void ? () => void : (val: EventValue) => void
7+
68
export type EventCallbackState<EventValue, State, Inputs = void> = [
7-
(val: EventValue) => void,
9+
VoidableCallback<EventValue>,
810
[State extends void ? null : State, BehaviorSubject<State | null>, BehaviorSubject<RestrictArray<Inputs> | null>]
911
]
1012
export type ReturnedState<EventValue, State, Inputs> = [
@@ -44,7 +46,9 @@ export function useEventCallback<EventValue, State = void, Inputs = void>(
4446
const inputSubject$ = new BehaviorSubject<RestrictArray<Inputs> | null>(typeof inputs === 'undefined' ? null : inputs)
4547
const stateSubject$ = new BehaviorSubject<State | null>(initialValue)
4648
const [state, setState] = useState(initialValue)
47-
const [returnedCallback, setEventCallback] = useState<(val: EventValue) => void>(() => noop)
49+
const [returnedCallback, setEventCallback] = useState<VoidableCallback<EventValue>>(
50+
() => noop as VoidableCallback<EventValue>,
51+
)
4852
const [state$] = useState(stateSubject$)
4953
const [inputs$] = useState(inputSubject$)
5054

@@ -58,7 +62,7 @@ export function useEventCallback<EventValue, State = void, Inputs = void>(
5862
return event$.next(e)
5963
}
6064
setState(initialValue)
61-
setEventCallback(() => eventCallback)
65+
setEventCallback(() => eventCallback as VoidableCallback<EventValue>)
6266
let value$: Observable<State>
6367

6468
if (!inputs) {

0 commit comments

Comments
 (0)