-
-
Notifications
You must be signed in to change notification settings - Fork 651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nav types: Add and demo RouteProp, to keep route-param types next to props #4430
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Type-tests | ||
|
||
This directory contains tests of our types. | ||
|
||
This code never gets run, but it does get type-checked. We use it to | ||
test that some of our more complex types behave in the way we expect. | ||
|
||
Mostly this means checking that using the types in certain ways that | ||
should give errors, does give errors. (Checking that things that | ||
should be OK, are OK, is generally well covered by where we simply use | ||
the types, in our main app code.) | ||
|
||
The key tool for confirming that something does give an error is to | ||
write `$FlowExpectedError`. As far as Flow is concerned (see [Flow | ||
docs][https://flow.org/en/docs/errors/]), this has exactly the same | ||
effect as `$FlowFixMe`: it suppresses the error, and conversely it | ||
means that if we ever stop getting an error at that spot, we start | ||
getting a warning about the unused suppression. | ||
|
||
The difference between `$FlowFixMe` and `$FlowExpectedError` is purely | ||
for the human reader: the latter communicates that the error is | ||
intended, not something we want to get rid of, and in particular that | ||
if it ever goes away then that's itself a problem which we should | ||
investigate. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Type-tests for navigation. | ||
* | ||
* @flow strict-local | ||
*/ | ||
|
||
import React, { type ComponentType } from 'react'; | ||
import { | ||
createStackNavigator, | ||
type StackNavigationProp, | ||
TransitionPresets, | ||
} from '@react-navigation/stack'; | ||
|
||
import { type RouteProp, type RouteParamsOf } from '../react-navigation'; | ||
|
||
/* eslint-disable flowtype/generic-spacing */ | ||
|
||
// Test that `RouteProp` gives route.params the right type. | ||
function testRouteParamTypes() { | ||
type ProfileProps = {| | ||
// skip navigation | ||
+route: RouteProp<'Profile', {| +userId: string |}>, | ||
|}; | ||
|
||
function Profile(props: ProfileProps) { | ||
const { params } = props.route; | ||
|
||
(params.userId: string); | ||
// $FlowExpectedError | ||
(params.userId: empty); | ||
|
||
(('a': string): typeof params.userId); | ||
// $FlowExpectedError | ||
(('a': mixed): typeof params.userId); | ||
|
||
// $FlowExpectedError | ||
params.nonsense; | ||
} | ||
} | ||
|
||
// Test that `RouteProp` gives type-checking of the route name | ||
// at the navigator. | ||
function testNavigatorTypes() { | ||
// (The setup of this one is a bit cumbersome because we need to set up | ||
// the navigator.) | ||
|
||
type ProfileProps = {| | ||
+navigation: NavigationProp<'Profile'>, | ||
+route: RouteProp<'Profile', {| +userId: string |}>, | ||
|}; | ||
declare var Profile: ComponentType<ProfileProps>; | ||
|
||
declare var Profile12: ComponentType<{| | ||
+navigation: NavigationProp<'Profile1'>, | ||
+route: RouteProp<'Profile2', {| +userId: string |}>, | ||
|}>; | ||
|
||
type NavParamList = {| | ||
Profile: RouteParamsOf<typeof Profile>, | ||
Profile1: RouteParamsOf<typeof Profile12>, | ||
Profile2: RouteParamsOf<typeof Profile12>, | ||
|}; | ||
|
||
// prettier-ignore | ||
type NavigationProp<+RouteName: $Keys<NavParamList> = $Keys<NavParamList>> = | ||
StackNavigationProp<NavParamList, RouteName>; | ||
|
||
const Stack = createStackNavigator<NavParamList, NavParamList, NavigationProp<>>(); | ||
|
||
<Stack.Navigator> | ||
{/* Happy case is happy */} | ||
<Stack.Screen name="Profile" component={Profile} /> | ||
{/* $FlowExpectedError - mismatch of name with route prop */} | ||
<Stack.Screen name="Profile1" component={Profile12} /> | ||
{/* Should error but doesn't! on mismatch of name with navigation prop */} | ||
<Stack.Screen name="Profile2" component={Profile12} /> | ||
</Stack.Navigator>; | ||
} |
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSX just hanging out here, with no
return
and norender
—totally valid, and I wouldn't change it at all, but I did a double-take! 😄