Skip to content

Commit

Permalink
fix: context types, clean up generics
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Sep 29, 2023
1 parent 7d7b03a commit 8eb02cc
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 114 deletions.
3 changes: 3 additions & 0 deletions examples/react/wip-with-framer-motion/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ const indexRoute = new Route({
const postsRoute = new Route({
getParentRoute: () => rootRoute,
path: 'posts',
beforeLoad: () => ({
test: true,
}),
loader: async ({ context: { loaderClient } }) => {
await loaderClient.load({ key: 'posts' })
},
Expand Down
40 changes: 13 additions & 27 deletions packages/react-router/src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
AnyPathParams,
Expand,
ResolveAllParams,
DeepMergeAll,
IsAny,
} from '@tanstack/router-core'

declare module '@tanstack/router-core' {
Expand Down Expand Up @@ -124,12 +126,15 @@ declare module '@tanstack/router-core' {
TParentRoute,
TParams
>,
TParentContext extends RouteConstraints['TParentContext'] = TParentRoute['types']['routeContext'],
TAllParentContext extends RouteConstraints['TAllParentContext'] = TParentRoute['types']['context'],
TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
TAllContext extends RouteConstraints['TAllContext'] = MergeFromFromParent<
TParentRoute['types']['context'],
TRouteContext
TAllContext extends RouteConstraints['TAllContext'] = Expand<
DeepMergeAll<
[
IsAny<TParentRoute['types']['context'], {}>,
TLoaderContext,
TRouteContext,
]
>
>,
TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
TChildren extends RouteConstraints['TChildren'] = unknown,
Expand All @@ -141,12 +146,9 @@ declare module '@tanstack/router-core' {
useLoader: <TSelected = TLoader>(opts?: {
select?: (search: TLoader) => TSelected
}) => UseLoaderResult<TSelected>
useContext: <TSelected = TAllContext>(opts?: {
useRouteContext: <TSelected = TAllContext>(opts?: {
select?: (search: TAllContext) => TSelected
}) => TSelected
useRouteContext: <TSelected = TRouteContext>(opts?: {
select?: (search: TRouteContext) => TSelected
}) => TSelected
useSearch: <TSelected = TFullSearchSchema>(opts?: {
select?: (search: TFullSearchSchema) => TSelected
}) => TSelected
Expand Down Expand Up @@ -208,12 +210,9 @@ export type RouteProps<
useMatch: <TSelected = TAllContext>(opts?: {
select?: (search: TAllContext) => TSelected
}) => TSelected
useContext: <TSelected = TAllContext>(opts?: {
useRouteContext: <TSelected = TAllContext>(opts?: {
select?: (search: TAllContext) => TSelected
}) => TSelected
useRouteContext: <TSelected = TRouteContext>(opts?: {
select?: (search: TRouteContext) => TSelected
}) => TSelected
useSearch: <TSelected = TFullSearchSchema>(opts?: {
select?: (search: TFullSearchSchema) => TSelected
}) => TSelected
Expand Down Expand Up @@ -265,19 +264,11 @@ Route.__onInit = (route) => {
useLoader: (opts = {}) => {
return useLoader({ ...opts, from: route.id }) as any
},
useContext: (opts: any = {}) => {
return useMatch({
...opts,
from: route.id,
select: (d: any) => (opts?.select ? opts.select(d.context) : d.context),
} as any)
},
useRouteContext: (opts: any = {}) => {
return useMatch({
...opts,
from: route.id,
select: (d: any) =>
opts?.select ? opts.select(d.routeContext) : d.routeContext,
select: (d: any) => (opts?.select ? opts.select(d.context) : d.context),
} as any)
},
useSearch: (opts = {}) => {
Expand Down Expand Up @@ -659,7 +650,6 @@ function Matches() {
return React.createElement(ErrorComponent, {
...props,
useMatch: route.useMatch,
useContext: route.useContext,
useRouteContext: route.useRouteContext,
useSearch: route.useSearch,
useParams: route.useParams,
Expand Down Expand Up @@ -999,7 +989,6 @@ function Match({ matchIds }: { matchIds: string[] }) {
return React.createElement(routeErrorComponent, {
...props,
useMatch: route.useMatch,
useContext: route.useContext,
useRouteContext: route.useRouteContext,
useSearch: route.useSearch,
useParams: route.useParams,
Expand All @@ -1013,7 +1002,6 @@ function Match({ matchIds }: { matchIds: string[] }) {
<ResolvedSuspenseBoundary
fallback={React.createElement(PendingComponent, {
useMatch: route.useMatch,
useContext: route.useContext,
useRouteContext: route.useRouteContext,
useSearch: route.useSearch,
useParams: route.useParams,
Expand Down Expand Up @@ -1059,7 +1047,6 @@ function MatchInner({
return React.createElement(PendingComponent, {
useLoader: route.useLoader,
useMatch: route.useMatch,
useContext: route.useContext,
useRouteContext: route.useRouteContext,
useSearch: route.useSearch,
useParams: route.useParams,
Expand All @@ -1073,7 +1060,6 @@ function MatchInner({
return React.createElement(comp, {
useLoader: route.useLoader,
useMatch: route.useMatch,
useContext: route.useContext as any,
useRouteContext: route.useRouteContext as any,
useSearch: route.useSearch,
useParams: route.useParams as any,
Expand Down
18 changes: 9 additions & 9 deletions packages/router-core/src/fileRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
TrimPathLeft,
RouteConstraints,
} from './route'
import { DeepMergeAll, Expand, IsAny } from './utils'

export interface FileRoutesByPath {
// '/': {
Expand Down Expand Up @@ -99,12 +100,15 @@ export class FileRoute<
TParentRoute['types']['allParams'],
TParams
>,
TParentContext extends RouteConstraints['TParentContext'] = TParentRoute['types']['routeContext'],
TAllParentContext extends RouteConstraints['TAllParentContext'] = TParentRoute['types']['context'],
TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
TContext extends RouteConstraints['TAllContext'] = MergeFromFromParent<
TParentRoute['types']['context'],
TRouteContext
TContext extends RouteConstraints['TAllContext'] = Expand<
DeepMergeAll<
[
IsAny<TParentRoute['types']['context'], {}>,
TLoaderContext,
TRouteContext,
]
>
>,
TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
TChildren extends RouteConstraints['TChildren'] = unknown,
Expand All @@ -121,8 +125,6 @@ export class FileRoute<
TFullSearchSchema,
TParams,
TAllParams,
TParentContext,
TAllParentContext,
TRouteContext,
TContext
>,
Expand All @@ -148,8 +150,6 @@ export class FileRoute<
TFullSearchSchema,
TParams,
TAllParams,
TParentContext,
TAllParentContext,
TRouteContext,
TContext,
TRouterContext,
Expand Down
Loading

0 comments on commit 8eb02cc

Please sign in to comment.