From b2367880cd0ca9054ea2762042fa3c6edc1af1d4 Mon Sep 17 00:00:00 2001 From: "satyajit.happy" Date: Wed, 11 Sep 2019 16:48:05 +0200 Subject: [PATCH] fix: fix type definitions --- .../example/src/SimpleStack.tsx | 7 +- .../src/StackWithCustomHeaderBackImage.tsx | 5 +- .../example/src/StackWithHeaderPreset.tsx | 10 +- .../src/StackWithTranslucentHeader.tsx | 3 +- .../typescript/react-navigation.d.ts | 126 +++++++----------- 5 files changed, 61 insertions(+), 90 deletions(-) diff --git a/packages/react-navigation/example/src/SimpleStack.tsx b/packages/react-navigation/example/src/SimpleStack.tsx index aebbace8..501a299d 100644 --- a/packages/react-navigation/example/src/SimpleStack.tsx +++ b/packages/react-navigation/example/src/SimpleStack.tsx @@ -21,7 +21,8 @@ import SampleText from './SampleText'; const DEBUG = false; interface MyNavScreenProps { - navigation: NavigationScreenProp; + // TODO: satya164 - use stack's navigation type + navigation: any; banner: React.ReactNode; } @@ -144,8 +145,10 @@ class MyHomeScreen extends React.Component { } interface MyPhotosScreenProps { - navigation: NavigationScreenProp; + // TODO: satya164 - use stack's navigation type + navigation: any; } + class MyPhotosScreen extends React.Component { static navigationOptions = { headerLeft: () => , diff --git a/packages/react-navigation/example/src/StackWithCustomHeaderBackImage.tsx b/packages/react-navigation/example/src/StackWithCustomHeaderBackImage.tsx index b20c4c4e..d98c463b 100644 --- a/packages/react-navigation/example/src/StackWithCustomHeaderBackImage.tsx +++ b/packages/react-navigation/example/src/StackWithCustomHeaderBackImage.tsx @@ -124,7 +124,10 @@ const StackWithCustomHeaderBackImage = createStackNavigator( }, }, { - defaultNavigationOptions: ({ theme }: NavigationScreenConfigProps) => ({ + // TODO: satya164 - use stack's navigation type + defaultNavigationOptions: ({ + theme, + }: NavigationScreenConfigProps) => ({ headerBackImage: ( ; + // TODO: satya164 - use stack's navigation type + navigation: any; } class HomeScreen extends React.Component { diff --git a/packages/react-navigation/example/src/StackWithTranslucentHeader.tsx b/packages/react-navigation/example/src/StackWithTranslucentHeader.tsx index 7d25f6e7..53354ff8 100644 --- a/packages/react-navigation/example/src/StackWithTranslucentHeader.tsx +++ b/packages/react-navigation/example/src/StackWithTranslucentHeader.tsx @@ -20,7 +20,8 @@ import { HeaderButtons } from './commonComponents/HeaderButtons'; import SampleText from './SampleText'; interface MyNavScreenProps { - navigation: NavigationScreenProp; + // TODO: satya164 - use stack's navigation type + navigation: any; banner: React.ReactNode; } diff --git a/packages/react-navigation/typescript/react-navigation.d.ts b/packages/react-navigation/typescript/react-navigation.d.ts index 50a3b97a..8599af4d 100644 --- a/packages/react-navigation/typescript/react-navigation.d.ts +++ b/packages/react-navigation/typescript/react-navigation.d.ts @@ -81,7 +81,7 @@ declare module 'react-navigation' { routes: NavigationRoute[]; isTransitioning: boolean; key: string; - params: NavigationParams | undefined; + params?: NavigationParams | undefined; } export interface DrawerNavigationState extends NavigationState { @@ -164,9 +164,9 @@ declare module 'react-navigation' { params?: NavigationParams; }; - getComponentForRouteName: (routeName: string) => NavigationComponent; + getComponentForRouteName: (routeName: string) => NavigationComponent<{}, NavigationScreenProp>; - getComponentForState: (state: State) => NavigationComponent; + getComponentForState: (state: State) => NavigationComponent<{}, NavigationScreenProp>; getActionCreators: ( route: NavigationRoute, @@ -186,50 +186,31 @@ declare module 'react-navigation' { getScreenOptions: NavigationScreenOptionsGetter; } - export type NavigationScreenOption = - | T - | ((navigation: NavigationScreenProp, config: T) => T); + export type NavigationScreenComponent = React.ComponentType & { + navigationOptions?: NavigationScreenConfig; + }; - export interface NavigationScreenDetails { - options: T; - state: NavigationRoute; - navigation: NavigationScreenProp; - } - - export interface NavigationScreenConfigProps { - navigation: NavigationScreenProp; + export interface NavigationScreenConfigProps { + navigation: NavigationScreenPropType; screenProps: unknown; theme: SupportedThemes; } - export type NavigationScreenConfig = + export type NavigationScreenConfig = | Options | (( - navigationOptionsContainer: NavigationScreenConfigProps & { - navigationOptions: NavigationScreenConfig; + navigationOptionsContainer: NavigationScreenConfigProps & { + navigationOptions: NavigationScreenConfig; } ) => Options); - export type NavigationComponent = - | NavigationScreenComponent - | NavigationNavigator - | any; + export type NavigationComponent = + | NavigationScreenComponent + | NavigationNavigator; - export type NavigationScreenComponent< - Params = NavigationParams, - Options = {}, - Props = {} - > = React.ComponentType & Props> & { - navigationOptions?: NavigationScreenConfig; - }; - - export type NavigationNavigator< - State = NavigationState, - Options = {}, - Props = {} - > = React.ComponentType & Props> & { - router: NavigationRouter; - navigationOptions?: NavigationScreenConfig; + export type NavigationNavigator = React.ComponentType> & { + router: NavigationRouter; + navigationOptions?: NavigationScreenConfig; }; export interface NavigationParams { @@ -414,7 +395,7 @@ declare module 'react-navigation' { } export function SwitchRouter( - routeConfigs: NavigationRouteConfigMap, + routeConfigs: NavigationRouteConfigMap, config?: NavigationSwitchRouterConfig ): NavigationRouter; @@ -452,20 +433,13 @@ declare module 'react-navigation' { | NavigationSwitchAction | { type: 'CHILD_ACTION'; key?: string }; - export type NavigationRouteConfig = - | NavigationComponent - | ({ - navigationOptions?: NavigationScreenConfig; + export type NavigationRouteConfig = + | NavigationComponent + | ( + | { screen: NavigationComponent } + | { getScreen(): NavigationScreenComponent }) & { + navigationOptions?: NavigationScreenConfig; path?: string; - } & NavigationScreenRouteConfig); - - export type NavigationScreenRouteConfig = - | NavigationComponent - | { - screen: NavigationComponent; - } - | { - getScreen: () => NavigationComponent; }; export interface NavigationPathsConfig { @@ -473,7 +447,7 @@ declare module 'react-navigation' { } // tslint:disable-next-line:strict-export-declare-modifiers - interface NavigationTabRouterConfig { + export interface NavigationTabRouterConfig { initialRouteName?: string; initialRouteParams?: NavigationParams; paths?: NavigationPathsConfig; @@ -482,8 +456,8 @@ declare module 'react-navigation' { resetOnBlur?: boolean; } - export interface NavigationRouteConfigMap { - [routeName: string]: NavigationRouteConfig; + export interface NavigationRouteConfigMap { + [routeName: string]: NavigationRouteConfig; } export type NavigationDispatch = (action: NavigationAction) => boolean; @@ -516,7 +490,6 @@ declare module 'react-navigation' { } export interface NavigationEventsProps extends ViewProps { - navigation?: NavigationNavigator; onWillFocus?: NavigationEventCallback; onDidFocus?: NavigationEventCallback; onWillBlur?: NavigationEventCallback; @@ -557,23 +530,11 @@ declare module 'react-navigation' { ): NonNullable; getParam(param: T): P[T]; setParams: (newParams: Partial

) => boolean; + emit: (eventName: 'refocus') => void; addListener: ( - eventName: 'willBlur' | 'willFocus' | 'didFocus' | 'didBlur', + eventName: string, callback: NavigationEventCallback ) => NavigationEventSubscription; - push: ( - routeName: string, - params?: NavigationParams, - action?: NavigationNavigateAction - ) => boolean; - replace: ( - routeName: string, - params?: NavigationParams, - action?: NavigationNavigateAction - ) => boolean; - reset: (actions: NavigationAction[], index: number) => boolean; - pop: (n?: number, params?: { immediate?: boolean }) => boolean; - popToTop: (params?: { immediate?: boolean }) => boolean; isFocused: () => boolean; isFirstRouteInParent: () => boolean; router?: NavigationRouter; @@ -655,7 +616,7 @@ declare module 'react-navigation' { export type _SwitchNavigatorConfig = NavigationSwitchRouterConfig; export function createSwitchNavigator( - routeConfigMap: NavigationRouteConfigMap, + routeConfigMap: NavigationRouteConfigMap>, switchConfig?: SwitchNavigatorConfig ): NavigationContainer; @@ -738,7 +699,7 @@ declare module 'react-navigation' { * @desc from react-navigation/src/routers/TabRouter.js */ export function TabRouter( - routeConfigs: NavigationRouteConfigMap, + routeConfigs: NavigationRouteConfigMap, config: NavigationTabRouterConfig ): NavigationRouter; @@ -748,7 +709,7 @@ declare module 'react-navigation' { * @desc from react-navigation/src/routers/StackRouter.js */ export function StackRouter( - routeConfigs: NavigationRouteConfigMap, + routeConfigs: NavigationRouteConfigMap, config?: NavigationTabRouterConfig ): NavigationRouter; @@ -766,11 +727,12 @@ declare module 'react-navigation' { */ export interface NavigationDescriptor< Params = NavigationParams, - Options = {} + Options = {}, + NavigationScreenPropType = NavigationScreenProp > { key: string; state: NavigationLeafRoute | NavigationStateRoute; - navigation: NavigationScreenProp; + navigation: NavigationScreenPropType; options: Options; getComponent: () => React.ComponentType; } @@ -783,6 +745,11 @@ declare module 'react-navigation' { } & NavigationInjectedProps >; + export type CreateNavigatorConfig = NavigatorConfig & RouterConfig & { + defaultNavigationOptions?: Options; + navigationOptions?: { [key: string]: any }; + } + export function createNavigator( view: NavigationView, router: NavigationRouter, @@ -798,8 +765,8 @@ declare module 'react-navigation' { * * @see https://github.com/react-navigation/react-navigation/blob/master/src/createNavigationContainer.js */ - export function createNavigationContainer( - Component: NavigationNavigator + export function createNavigationContainer( + Component: NavigationNavigator ): NavigationContainer; /** @@ -807,8 +774,8 @@ declare module 'react-navigation' { * * @see https://github.com/react-navigation/react-navigation-native/blob/098e2e52b349d37357109d5aee545fa74699d3d4/src/createAppContainer.js#L64 */ - export function createAppContainer( - Component: NavigationNavigator + export function createAppContainer( + Component: NavigationNavigator ): NavigationContainer; /** @@ -821,11 +788,12 @@ declare module 'react-navigation' { export interface NavigationScreenProps< Params = NavigationParams, - Options = any + Options = {}, + NavigationScreenPropType = NavigationScreenProp > { navigation: NavigationScreenProp, Params>; screenProps?: unknown; - navigationOptions?: NavigationScreenConfig; + navigationOptions?: NavigationScreenConfig; } /**