Skip to content
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

fix: wrong setParams type if route does not have params #10512

Merged
merged 2 commits into from
Aug 7, 2022

Conversation

Gustash
Copy link
Contributor

@Gustash Gustash commented Apr 13, 2022

Motivation

Fixes #10471

If a RouteList type has a route with params set to undefined, for example:

type ExampleRouteList = {
  HasArgs: {someArg?: string};
  NoArgs: undefined;
};

Then navigation.setParams for NoArgs would be typed as (params: Partial<unknown>): void, since NavigationProp would type it as (params: Partial<RouteList[RouteName]>): void.

This would cause issues with CompositeScreenProps because a route with undefined params would not extend the NavigationProp type correctly, so that screen would not actually get all the options the parent navigators have available.

Conditionally setting setParams to undefined if RouteList[RouteName] extends undefined fixes this issue.

Test plan

Adding a test case in example/types.check.tsx for LatestScreen, which has params set to undefined, showed that this was indeed an issue because setOptions would fail the checks.

After the fix, these checks now pass for all screens, including the LatestScreen.

I also fixed the setOptions type checks so that they explicitly check that all the parent navigator screen options are available, instead of just a hand-full.

Code formatting

yarn test, yarn lint and yarn typescript all pass with these changes, and I have ran them locally before commiting.

If a RouteList type has a route with params set to undefined, for example:

```ts
type ExampleRouteList = {
  HasArgs: {someArg?: string};
  NoArgs: undefined;
};
```

Then `navigation.setParams` for `NoArgs` would be typed as `(params: Partial<unknown>): void`, since
`NavigationProp` would type it as `(params: Partial<RouteList[RouteName]>): void`.

This would cause issues with `CompositeScreenProps` because a route with `undefined` params
would not extend the `NavigationProp` type correctly, so that screen would not actually get
all the options the parent navigators have available.

Conditionally setting `setParams` to `undefined` if `RouteList[RouteName]` extends `undefined`
fixes this issue.
@github-actions
Copy link

Hey Gustash! Thanks for opening your first pull request in this repo. If you haven't already, make sure to read our contribution guidelines.

@netlify
Copy link

netlify bot commented Apr 13, 2022

Deploy Preview for react-navigation-example ready!

Name Link
🔨 Latest commit 8b279df
🔍 Latest deploy log https://app.netlify.com/sites/react-navigation-example/deploys/6256f6ec77e7770009a0d3bc
😎 Deploy Preview https://deploy-preview-10512--react-navigation-example.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@Gustash
Copy link
Contributor Author

Gustash commented May 5, 2022

@satya164 can I get a review here? Shouldn't take too long

@cjhines
Copy link

cjhines commented May 20, 2022

Waiting on this one.

@satya164 satya164 closed this Aug 7, 2022
@satya164 satya164 reopened this Aug 7, 2022
@codecov-commenter
Copy link

Codecov Report

Merging #10512 (8b279df) into main (b1c4214) will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##             main   #10512   +/-   ##
=======================================
  Coverage   73.87%   73.87%           
=======================================
  Files         160      160           
  Lines        4992     4992           
  Branches     1949     1949           
=======================================
  Hits         3688     3688           
  Misses       1267     1267           
  Partials       37       37           

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@satya164 satya164 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR

@satya164 satya164 merged commit 8ed42cd into react-navigation:main Aug 7, 2022
@Gustash Gustash deleted the fix/composite-screen-props branch August 7, 2022 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong CompositeNavigationProp resulting TS type
4 participants