just use typeof

This commit is contained in:
Samuel Newman
2025-05-28 13:38:39 +03:00
parent 134c7b7427
commit 571d16e8cf
+6 -41
View File
@@ -1,10 +1,8 @@
import {useMemo} from 'react' import {useMemo} from 'react'
import {useNavigation} from '@react-navigation/core' import {useNavigation} from '@react-navigation/core'
import {type NavigationState} from '@react-navigation/native'
import {type NavigationAction} from '@react-navigation/routers'
import {useDedupe} from '#/lib/hooks/useDedupe' import {useDedupe} from '#/lib/hooks/useDedupe'
import {type AllNavigatorParams, type NavigationProp} from '#/lib/routes/types' import {type NavigationProp} from '#/lib/routes/types'
export type DebouncedNavigationProp = Pick< export type DebouncedNavigationProp = Pick<
NavigationProp, NavigationProp,
@@ -24,50 +22,17 @@ export function useNavigationDeduped() {
return useMemo<DebouncedNavigationProp>( return useMemo<DebouncedNavigationProp>(
() => ({ () => ({
// Types from @react-navigation/routers/src/StackRouter.ts push: (...args: Parameters<typeof navigation.push>) => {
push: <RouteName extends keyof AllNavigatorParams>(
...args: {
[Screen in keyof AllNavigatorParams]: undefined extends AllNavigatorParams[Screen]
?
| [screen: Screen]
| [screen: Screen, params: AllNavigatorParams[Screen]]
: [screen: Screen, params: AllNavigatorParams[Screen]]
}[RouteName]
) => {
// @ts-expect-error can't get the types to work -sfn
dedupe(() => navigation.push(...args)) dedupe(() => navigation.push(...args))
}, },
// Types from @react-navigation/core/src/types.tsx navigate: (...args: Parameters<typeof navigation.navigate>) => {
navigate: <RouteName extends keyof AllNavigatorParams>(
...args: RouteName extends unknown
? undefined extends AllNavigatorParams[RouteName]
?
| [screen: RouteName]
| [screen: RouteName, params: AllNavigatorParams[RouteName]]
: [screen: RouteName, params: AllNavigatorParams[RouteName]]
: never
) => {
dedupe(() => navigation.navigate(...args)) dedupe(() => navigation.navigate(...args))
}, },
// Types from @react-navigation/routers/src/StackRouter.ts replace: (...args: Parameters<typeof navigation.replace>) => {
replace: <RouteName extends keyof AllNavigatorParams>(
...args: {
[Screen in keyof AllNavigatorParams]: undefined extends AllNavigatorParams[Screen]
?
| [screen: Screen]
| [screen: Screen, params: AllNavigatorParams[Screen]]
: [screen: Screen, params: AllNavigatorParams[Screen]]
}[RouteName]
) => {
// @ts-expect-error can't get the types to work -sfn
dedupe(() => navigation.replace(...args)) dedupe(() => navigation.replace(...args))
}, },
dispatch: ( dispatch: (...args: Parameters<typeof navigation.dispatch>) => {
action: dedupe(() => navigation.dispatch(...args))
| NavigationAction
| ((state: NavigationState) => NavigationAction),
) => {
dedupe(() => navigation.dispatch(action))
}, },
popToTop: () => { popToTop: () => {
dedupe(() => navigation.popToTop()) dedupe(() => navigation.popToTop())