Last active
March 15, 2022 06:26
-
-
Save listiani13/98090419db716f3ec1508a808e5e8a1f to your computer and use it in GitHub Desktop.
Workaround for react-navigation linking configuration typing issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NavigatorScreenParams } from '@react-navigation/native'; | |
import { Screen } from './screens'; | |
export declare type PathConfigWrapped<ParamList extends unknown> = { | |
path?: string; | |
exact?: boolean; | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
parse?: Record<string, (value: string) => any>; | |
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |
stringify?: Record<string, (value: any) => string>; | |
screens: PathConfigMapWrapped<ParamList>; | |
initialRouteName?: keyof ParamList; | |
}; | |
export type PathConfigMapWrapped<ParamList extends unknown> = { | |
[RouteName in keyof ParamList]: ParamList[RouteName] extends NavigatorScreenParams< | |
infer T, | |
infer _F | |
> | |
? string | PathConfigWrapped<T> | |
: string | Omit<PathConfigWrapped<unknown>, 'screens' | 'initialRouteName'>; | |
}; | |
type MockParam = { | |
autopay: NavigatorScreenParams<{ | |
screenB: NavigatorScreenParams<{ screenD: undefined }>; | |
screenC: undefined; | |
}>; | |
}; | |
type MockChildParam = { | |
childScreen: NavigatorScreenParams<MockParam>; | |
}; | |
type MockNestedParam = { | |
rootScreen: NavigatorScreenParams<MockChildParam>; | |
}; | |
// This is intended for testing the wrapped type | |
export const TestNestedScreens: PathConfigMapWrapped<MockNestedParam> = { | |
rootScreen: { | |
screens: { | |
childScreen: { | |
screens: { | |
autopay: { | |
// @ts-expect-error We expect this to throw type error, because we need to also define config for screenC | |
screens: { | |
screenB: { screens: { screenD: '' } }, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment