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

feat: add generated types for Expo Router useSegment #22629

Merged
merged 11 commits into from
Jul 3, 2023

Conversation

marklawlor
Copy link
Contributor

Why

  • Add type generation for Expo Router's useSegment
  • Adds code comments and renames a few internal types.
  • Refactored the useSearchParams types
  • Add default to Href<T>

Note that this is not a breaking change as the generated types are only present inside the users' cache and should not be committed. Next time the user starts the dev server they will get the new generated types.

Questions

@EvanBacon Given the route /(app)/note/[note]/ does useSegments return the group?

useSearchParams

This hook was a bit strange as its signature changed when you enabled types routes.

Without: useSearchParams<T extends Record<string, string>(): T
With: useSearchParams<T extends Route>(): SearchParams<T>

This PR updates the TypedRoutes version to accept either a Route or a Record. This Record is still typed checked and it must satisfy a possible SearchParams object.

For example

// This will only work if you have a [test] route somewhere in your app
const { test } = useSearchParams<{ test: string }>()

I quite like this as it allows you to cherry-pick the param that you need.

Note that this obviously doesn't do a runtime check for it maybe be correct, but its a start.

Href

Href has a default value and by default now represents all possible Hrefs. You can use the generic to reduce it down.

@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label May 24, 2023
@marklawlor marklawlor changed the title feat: add generated types for useSegment feat: add generated types for Expo Router useSegment May 24, 2023
@marklawlor marklawlor force-pushed the marklawlor/cli/typed-routes/useSegment branch from 9c8df0c to eed28b1 Compare May 24, 2023 06:46
@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels May 24, 2023
Copy link
Contributor

@EvanBacon EvanBacon left a comment

Choose a reason for hiding this comment

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

Approve pending better test coverage.

@marklawlor marklawlor force-pushed the marklawlor/cli/typed-routes/useSegment branch 4 times, most recently from b253fc4 to da7e922 Compare June 24, 2023 10:29
@marklawlor marklawlor force-pushed the marklawlor/cli/typed-routes/useSegment branch from ed8eb1c to 42f7607 Compare June 24, 2023 22:39
@marklawlor
Copy link
Contributor Author

Some updates

  • Search params and hashs are no longer valid in the Href form. TypeScript is unable to validate if they are correct, so you much the the HrefObject notation
  • External Urls are valid Href's
  • The logic for OptionalCatchAllRoutePart has been added but is unused. This means CatchAll routes are currently required when using the Href version, but are optional when using HrefObject (you can supply an empty array
    • I think this is the best approach until we introduce the syntax for optional catch all routes

@EvanBacon EvanBacon merged commit b31f747 into main Jul 3, 2023
16 checks passed
@EvanBacon EvanBacon deleted the marklawlor/cli/typed-routes/useSegment branch July 3, 2023 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: passed checks ExpoBot has nothing to complain about
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants