BG

Fix TypeScript Errors for Query Strings in SvelteKit's `resolve`

1m

SvelteKit's resolve from $app/paths only accepts known route strings. Append a query string and TypeScript will complain:

goto(resolve(`/some-route?${searchParams.toString()}`))
Argument of type '[`/some-route?${string}`]' is not assignable to parameter of type '[route: "/some-route"] | ...'

The template literal produces a type of `/some-route?${string}` which no longer matches the narrowly typed route union. The fix is a simple as cast back to the base route:

goto(resolve(`/some-route?${searchParams.toString()}` as '/some-route'))

This tells TypeScript to treat the argument as the plain route while keeping the dynamic query string at runtime.

Comment on twitter