Skip to content

Commit

Permalink
refactor: ensure TS builds fine in generic context
Browse files Browse the repository at this point in the history
  • Loading branch information
TylorS committed Feb 27, 2024
1 parent 6d2e1de commit 196e410
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
13 changes: 7 additions & 6 deletions packages/path/src/Interpolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @since 1.0.0
*/

import type { S } from "ts-toolbelt"
import type { A, S } from "ts-toolbelt"
import type {
ModifierNode,
NamedParamNode,
Expand All @@ -25,12 +25,13 @@ import type { PathJoin } from "./PathJoin.js"
* Interpolate a path with parameters
* @since 1.0.0
*/
export type Interpolate<P extends string, Params extends ParamsOf<P>> = PathJoin<
InterpolateParts<
ParseSegments<PathToSegments<P>>,
Params
export type Interpolate<P extends string, Params extends ParamsOf<P>> = A.Equals<P, string> extends 1 ? string :
PathJoin<
InterpolateParts<
ParseSegments<PathToSegments<P>>,
Params
>
>
>

type InterpolateParts<
T extends ReadonlyArray<any>,
Expand Down
3 changes: 2 additions & 1 deletion packages/path/src/ParamsOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @since 1.0.0
*/

import type { A } from "ts-toolbelt"
import type {
Modifier,
ModifierNode,
Expand All @@ -24,7 +25,7 @@ import type {
* Extract the parameters from a path
* @since 1.0.0
*/
export type ParamsOf<T extends string> = ToParams<ParseSegments<PathToSegments<T>>>
export type ParamsOf<T extends string> = A.Equals<T, string> extends 1 ? {} : ToParams<ParseSegments<PathToSegments<T>>>

type ToParams<T extends ReadonlyArray<any>, Params = {}> = [
// @ts-expect-error Type potentially infinite
Expand Down
13 changes: 8 additions & 5 deletions packages/router/src/CurrentRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function layer<const P extends string>(
route: P | Route.Route<P>,
parent: Option.Option<CurrentRoute> = Option.none()
): Layer.Layer<CurrentRoute> {
return CurrentRoute.layer(make(route as Route.Route<string>, parent))
return CurrentRoute.layer(make(route, parent) as any as CurrentRoute)
}

function getRoute<P extends string>(route: P | Route.Route<P>): Route.Route<P> {
Expand All @@ -59,9 +59,12 @@ function getRoute<P extends string>(route: P | Route.Route<P>): Route.Route<P> {
/**
* @since 1.0.0
*/
export const CurrentParams: RefSubject.Filtered<Readonly<Record<string, string>>, never, Navigation | CurrentRoute> =
RefSubject
.filterMapEffect(CurrentPath, (path) => CurrentRoute.with(({ route }) => route.match(path)))
export const CurrentParams: RefSubject.Filtered<
Readonly<Record<string, string | ReadonlyArray<string>>>,
never,
Navigation | CurrentRoute
> = RefSubject
.filterMapEffect(CurrentPath, (path) => CurrentRoute.with(({ route }) => route.match(path)))

/**
* @since 1.0.0
Expand All @@ -82,7 +85,7 @@ export const withCurrentRoute: {
Effect.contextWithEffect((ctx) => {
const parent = Context.getOption(ctx, CurrentRoute)

if (Option.isNone(parent)) return pipe(effect, CurrentRoute.provide(make(route) as CurrentRoute))
if (Option.isNone(parent)) return pipe(effect, CurrentRoute.provide(make(route) as any as CurrentRoute))

return pipe(
effect,
Expand Down
4 changes: 2 additions & 2 deletions packages/router/src/Matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ function getGuard<const P extends string, B, E2, R2>(
guard?: Guard.Guard<Path.ParamsOf<P>, B, E2, R2>
) {
if (guard) {
return Guard.compose(getRoute(path), guard)
return Guard.compose(path, guard)
} else {
return getRoute(path).asGuard()
return path.asGuard()
}
}

Expand Down

0 comments on commit 196e410

Please sign in to comment.