You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In larger projects (also in smaller projects) it would be great if the goto and the fetch functions could offer more typesafety. Problems with missing typesafety are:
typos when you link to a route that does't exist e.g. /costumer instead of /customer
help for refactorings e.g. when renaming larger chunks of files inside the routes folder, all links need to be updated too
trying to call fetch with the wrong method e.g. using PUT instead of PATCH
It would be great if the goto and the fetch functions could output an error when you pass in a invalid relative slug.
Describe the proposed solution
The problem could be solved by providing advanced TypeScript types for the goto and the fetch function.
Similar tho the already generated .svelte-kit/dev/generated/manifest.js file, SvelteKit could generate a d.ts with types depending on the .svelte files inside the routes folder and depending on the function inside a .js and .ts Endpoints file.
These types then could be used to enhance the goto and fetch functions.
The typesafe functions could replace the existing import from app/navigation. I'm not sure how this could work for the fetch function since you don't really import it from anywhere.
Or this could be an additional function you need to import from app/typesafe or something similar.
Here is a working example how I think this could look like:
// alias type to get better TypeScript hints inside tooltipstypeid=string// this type is dynamic and get's generatedtypeRoutes=|['/']// index.svelte|['/','about']// about/index.svelte|['/','products']// products/index.svelte|['/','products','/','create']// products/index.svelte|['/','products','/',id]// products/[id]/index.svelte|['/','products','/',id,'/','edit']// products/[id]/edit.svelteexporttypeIsValidRoute<Rextendsstring>=Rextends `http${string}`
? R
: RemoveEmptyEntries<SplitPath<R>>extendsRoutes
? R
: 'No such Route'constgoto=<Routeextendsstring>(href: IsValidRoute<Route>): void=>{// TODO: goto href}// @ts-expect-errorgoto('')goto('/')goto('/about')// @ts-expect-errorgoto('/invalid')// @ts-expect-errorgoto('/product')goto('/products')// @ts-expect-errorgoto('/products/')// @ts-expect-errorgoto('/products/create/')goto('/products/123456')// @ts-expect-errorgoto('/products/123456/')// @ts-expect-errorgoto('/products/123456/add')goto('/products/123456/edit')// @ts-expect-errorgoto('/products/123456/5678/edit')goto('https://kit.svelte.dev')
endpoints: for the fetch function
typeMethods=|'GET'|'POST'|'PUT'|'PATCH'|'DELETE'// this type is dynamic and get's generatedtypeEndpoints={GET:
|['/','products']|['/','products','/',id]POST:
|['/','products']PATCH:
|['/','products','/',id]}exporttypeIsValidEndpoint<MextendsMethods,Rextendsstring>=Rextends `http${string}`
? R
: MextendskeyofEndpoints
? RemoveEmptyEntries<SplitPath<R>>extendsEndpoints[M]
? R
: 'No such Endpoint'
: 'No such Method'constfetch=<Endpointextendsstring,MethodextendsMethods='GET'>(endpoint: IsValidEndpoint<Method,Endpoint>,options?: {method?: Method,[key: string]: any}): void=>{// TODO: call fetch}fetch('/products')// @ts-expect-errorfetch('products')fetch('/products/12345')fetch('/products',{method: 'POST'})// @ts-expect-errorfetch('/products',{method: 'PATCH'})// @ts-expect-errorfetch('/products/12345',{method: 'POST'})fetch('/products/12345',{method: 'PATCH'})fetch('http://example.com/articles')
You can copy these examples to a .ts file and try passing some valid/invalid strings to the goto and fetch functions.
Lines annotated with // @ts-expect-error are invalid and will throw a TypeScript Error.
Alternatives considered
Don't offer typesafe routes and api-endpoints.
Importance
would make my life easier
Additional Information
If adding this feature is considered, I'm happy to help creating a PR.
The text was updated successfully, but these errors were encountered:
Describe the problem
In larger projects (also in smaller projects) it would be great if the
goto
and thefetch
functions could offer more typesafety. Problems with missing typesafety are:/costumer
instead of/customer
routes
folder, all links need to be updated toofetch
with the wrong method e.g. usingPUT
instead ofPATCH
It would be great if the
goto
and thefetch
functions could output an error when you pass in a invalid relative slug.Describe the proposed solution
The problem could be solved by providing advanced TypeScript types for the
goto
and thefetch
function.Similar tho the already generated
.svelte-kit/dev/generated/manifest.js
file, SvelteKit could generate ad.ts
with types depending on the.svelte
files inside theroutes
folder and depending on the function inside a.js
and.ts
Endpoints file.These types then could be used to enhance the
goto
andfetch
functions.The typesafe functions could replace the existing import from
app/navigation
. I'm not sure how this could work for thefetch
function since you don't really import it from anywhere.Or this could be an additional function you need to import from
app/typesafe
or something similar.Here is a working example how I think this could look like:
goto
functionfetch
functionYou can copy these examples to a
.ts
file and try passing some valid/invalid strings to thegoto
andfetch
functions.Lines annotated with
// @ts-expect-error
are invalid and will throw a TypeScript Error.Alternatives considered
Don't offer typesafe routes and api-endpoints.
Importance
would make my life easier
Additional Information
If adding this feature is considered, I'm happy to help creating a PR.
The text was updated successfully, but these errors were encountered: