Skip to content

Commit

Permalink
Fix the type error with possibly infinite deep
Browse files Browse the repository at this point in the history
  • Loading branch information
pleek91 committed Dec 30, 2023
1 parent 2937821 commit 0248ecc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/utilities/createRouteMethods.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('createRouteMethods', () => {
] as const satisfies Routes
const resolved = resolveRoutes(routes)

const response = createRouteMethods(resolved)
const response = createRouteMethods<typeof routes>(resolved)

expect(response.parent).toBeTypeOf('function')
})
Expand All @@ -34,7 +34,7 @@ describe('createRouteMethods', () => {
] as const satisfies Routes
const resolved = resolveRoutes(routes)

const response = createRouteMethods(resolved)
const response = createRouteMethods<typeof routes>(resolved)

expect(response).toMatchObject({})
})
Expand All @@ -60,7 +60,7 @@ describe('createRouteMethods', () => {
] as const satisfies Routes
const resolved = resolveRoutes(routes)

const response = createRouteMethods(resolved)
const response = createRouteMethods<typeof routes>(resolved)

expect(response.parent.child).toBeDefined()

Expand Down Expand Up @@ -91,7 +91,7 @@ describe('createRouteMethods', () => {
] as const satisfies Routes
const resolved = resolveRoutes(routes)

const response = createRouteMethods(resolved)
const response = createRouteMethods<typeof routes>(resolved)

expect(response.parent).toBeTypeOf('function')
expect(response.parent.child).toBeTypeOf('function')
Expand Down
6 changes: 3 additions & 3 deletions src/utilities/createRouteMethods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Resolved, Route, RouteMethodResponse, isPublicRoute } from '@/types'
import { Resolved, Route, RouteMethodResponse, RouteMethods, Routes, isPublicRoute } from '@/types'
import { assembleUrl } from '@/utilities/urlAssembly'

type NonCallableNode = Record<string, any>
Expand All @@ -7,14 +7,14 @@ type CallableNode = NonCallableNode & {
}
type Node = CallableNode | NonCallableNode

export function createRouteMethods(routes: Resolved<Route>[]): Record<string, Node> {
export function createRouteMethods<T extends Routes>(routes: Resolved<Route>[]): RouteMethods<T> {
const methods: Record<string, any> = {}

routes.forEach(route => {
traverseParents(route, methods)
})

return methods
return methods as unknown as RouteMethods<T>
}

function traverseParents(route: Resolved<Route>, currentLevel: Record<string, any>): Record<string, any> {
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/createRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function createRouter<T extends Routes>(routes: T): Router<T> {
}

const router = {
routes: createRouteMethods(resolved) as RouteMethods<T>,
routes: createRouteMethods<T>(resolved),
push,
replace,
forward,
Expand Down

0 comments on commit 0248ecc

Please sign in to comment.