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: fix types for relative routing #3084

Merged
merged 6 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions packages/react-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,7 @@
"zod": "^3.23.8"
},
"peerDependencies": {
"@tanstack/router-generator": "workspace:^",
"react": ">=18",
"react-dom": ">=18"
},
"peerDependenciesMeta": {
"@tanstack/router-generator": {
"optional": true
}
}
}
113 changes: 64 additions & 49 deletions packages/react-router/src/link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
RouteByToPath,
RoutePaths,
RouteToPath,
ToPath,
TrailingSlashOptionByRouter,
} from './routeInfo'
import type {
Expand Down Expand Up @@ -59,63 +60,70 @@ export type ParsePathParams<T extends string, TAcc = never> = T &
: never
: TAcc

export type AddTrailingSlash<T> = T & `${string}/` extends never
? `${T & string}/`
: T
export type AddTrailingSlash<T> = T extends `${string}/` ? T : `${T & string}/`

export type RemoveTrailingSlashes<T> = T & `${string}/` extends never
? T
: T extends `${infer R}/`
export type RemoveTrailingSlashes<T> = T extends `${string}/`
? T extends `${infer R}/`
? R
: T

export type AddLeadingSlash<T> = T & `/${string}` extends never
? `/${T & string}`
: T

export type RemoveLeadingSlashes<T> = T & `/${string}` extends never
? T
: T extends `/${infer R}`
export type AddLeadingSlash<T> = T extends `/${string}` ? T : `/${T & string}`

export type RemoveLeadingSlashes<T> = T extends `/${string}`
? T extends `/${infer R}`
? R
: T
: T

export type FindDescendantPaths<
TRouter extends AnyRouter,
TPrefix extends string,
> = `${TPrefix}${string}` & RouteToPath<TRouter>
> = `${TPrefix}/${string}` & RouteToPath<TRouter>

export type SearchPaths<
TRouter extends AnyRouter,
TPrefix extends string,
TPaths = FindDescendantPaths<TRouter, TPrefix>,
> = TPaths extends `${TPrefix}${infer TRest}` ? TRest : never
> = TPaths extends `${TPrefix}/`
? never
: TPaths extends `${TPrefix}/${infer TRest}`
? TRest
: never

export type SearchRelativePathAutoComplete<
TRouter extends AnyRouter,
TTo extends string,
TSearchPath extends string,
> = `${TTo}${SearchPaths<TRouter, TSearchPath>}`
> =
| (TSearchPath & RouteToPath<TRouter> extends never
? never
: ToPath<TrailingSlashOptionByRouter<TRouter>, TTo>)
| `${TTo}/${SearchPaths<TRouter, RemoveTrailingSlashes<TSearchPath>>}`

export type RelativeToParentPathAutoComplete<
TRouter extends AnyRouter,
TFrom extends string,
TTo extends string,
TResolvedPath extends string = RemoveTrailingSlashes<
ResolveRelativePath<TFrom, TTo>
>,
TResolvedPath extends string = ResolveRelativePath<TFrom, TTo>,
> =
| SearchRelativePathAutoComplete<TRouter, TTo, TResolvedPath>
| (TResolvedPath extends ''
? never
: `${TTo}/${ParentPath<TrailingSlashOptionByRouter<TRouter>>}`)
| (TTo extends `${string}..` | `${string}../`
? TResolvedPath extends '/' | ''
? never
: FindDescendantPaths<
TRouter,
RemoveTrailingSlashes<TResolvedPath>
> extends never
? never
: `${TTo}/${ParentPath<TrailingSlashOptionByRouter<TRouter>>}`
: never)

export type RelativeToCurrentPathAutoComplete<
TRouter extends AnyRouter,
TFrom extends string,
TTo extends string,
TResolvedPath extends string = RemoveTrailingSlashes<
ResolveRelativePath<TFrom, TTo>
>,
TResolvedPath extends string = ResolveRelativePath<TFrom, TTo>,
> =
| SearchRelativePathAutoComplete<TRouter, TTo, TResolvedPath>
| CurrentPath<TrailingSlashOptionByRouter<TRouter>>
Expand All @@ -128,9 +136,7 @@ export type AbsolutePathAutoComplete<
? CurrentPath<TrailingSlashOptionByRouter<TRouter>>
: TFrom extends `/`
? never
: FindDescendantPaths<TRouter, TFrom> extends never
? never
: CurrentPath<TrailingSlashOptionByRouter<TRouter>>)
: CurrentPath<TrailingSlashOptionByRouter<TRouter>>)
| (string extends TFrom
? ParentPath<TrailingSlashOptionByRouter<TRouter>>
: TFrom extends `/`
Expand All @@ -141,7 +147,7 @@ export type AbsolutePathAutoComplete<
? never
: string extends TFrom
? never
: RemoveLeadingSlashes<SearchPaths<TRouter, TFrom>>)
: SearchPaths<TRouter, RemoveTrailingSlashes<TFrom>>)

export type RelativeToPathAutoComplete<
TRouter extends AnyRouter,
Expand Down Expand Up @@ -485,46 +491,55 @@ type RemoveLastSegment<
TAcc extends string = '',
> = T extends `${infer TSegment}/${infer TRest}`
? TRest & `${string}/${string}` extends never
? `${TAcc}${TSegment}`
? TRest extends ''
? TAcc
: `${TAcc}${TSegment}`
: RemoveLastSegment<TRest, `${TAcc}${TSegment}/`>
: TAcc

export type ResolveCurrentPath<TFrom, TTo> = TTo extends '.'
export type ResolveCurrentPath<
TFrom extends string,
TTo extends string,
> = TTo extends '.'
? TFrom
: TTo extends './'
? AddTrailingSlash<TFrom>
: TTo & `./${string}` extends never
? never
: TTo extends `./${infer TRest}`
? ResolveRelativePath<TFrom, TRest>
? AddLeadingSlash<JoinPath<TFrom, TRest>>
: never

export type ResolveParentPath<TFrom extends string, TTo> = TTo extends '../'
? RemoveLastSegment<TFrom>
: TTo & `..${string}` extends never
export type ResolveParentPath<
TFrom extends string,
TTo extends string,
> = TTo extends '../' | '..'
? TFrom extends '' | '/'
? never
: TTo extends `..${infer ToRest}`
? ResolveRelativePath<
RemoveLastSegment<TFrom>,
RemoveLeadingSlashes<ToRest>
>
: never
: AddLeadingSlash<RemoveLastSegment<TFrom>>
: TTo & `../${string}` extends never
? AddLeadingSlash<JoinPath<TFrom, TTo>>
: TFrom extends '' | '/'
? never
: TTo extends `../${infer ToRest}`
? ResolveParentPath<RemoveLastSegment<TFrom>, ToRest>
: AddLeadingSlash<JoinPath<TFrom, TTo>>

export type ResolveRelativePath<TFrom, TTo = '.'> = string extends TFrom
? TTo
: string extends TTo
? TFrom
: undefined extends TTo
? TFrom
: TFrom extends string
? TTo extends string
? TTo & `..${string}` extends never
? TTo & `.${string}` extends never
? TTo & `/${string}` extends never
? AddLeadingSlash<JoinPath<TFrom, TTo>>
: TTo
: ResolveCurrentPath<TFrom, TTo>
: ResolveParentPath<TFrom, TTo>
: TTo extends string
? TFrom extends string
? TTo extends `/${string}`
? TTo
: TTo extends `..${string}`
? ResolveParentPath<TFrom, TTo>
: TTo extends `.${string}`
? ResolveCurrentPath<TFrom, TTo>
: AddLeadingSlash<JoinPath<TFrom, TTo>>
: never
: never

Expand Down
6 changes: 6 additions & 0 deletions packages/react-router/src/routeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export type CurrentPath<TOption> = 'always' extends TOption
? '.'
: './' | '.'

export type ToPath<TOption, TTo extends string> = 'always' extends TOption
? `${TTo}/`
: 'never' extends TOption
? TTo
: TTo | `${TTo}/`

export type CatchAllPaths<TOption> = CurrentPath<TOption> | ParentPath<TOption>

export type CodeRoutesByPath<TRouteTree extends AnyRoute> =
Expand Down
Loading
Loading