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

infer useMatchesData type using generic #281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zwhitchcox
Copy link

@zwhitchcox zwhitchcox commented Apr 18, 2024

This PR makes it possible to infer the returned type of useMatchesData with a generic type.

This makes it possible to export a hook from a layout route:

// app/routes/book.tsx
function useServices() {
	const { services } = useMatchesData<typeof loader>('routes/book')
	return services
}

and automatically infer the data in a child route:

// app/routes/book.schedule.tsx
const services = useServices() // services is typed correctly

This will not be a breaking change, because the default type will be the same as the currently cast type (Record<string, unknown>).

@zwhitchcox
Copy link
Author

Sidenote: I know you could use useOutletContext with SerializeFrom, and that would achieve the same thing, but then you have to import the loader as somethingLoader, pass it to SerializeFrom, and it wouldn't work with deeply nested routes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant