Generated types for pages/endpoints
See original GitHub issueWays that types in SvelteKit apps could be improved:
Implicit params and props for load functions (update: done)
<script context="module">
/** @type {import('@sveltejs/kit').Load */
export async function load({ params, fetch }) {
// `params` automatically typed from filename (e.g. `src/routes/blog/[slug]`)
const res = await fetch(`/blog/${params.slug}.json`);
const { post } = await res.json();
return {
props: {
// `post` type automatically inferred from component props
post
}
};
}
</script>
<script>
/** @type {BlogPost} */
export let post;
</script>
Similarly, with shadow endpoints, it would be good to type body based on component props (though this could be tricky since component props combine e.g. post and get bodies), and also type the props input to load in cases where it’s used.
It might be possible to do something clever with rootDirs, or with preprocessors?
Typed goto and fetch
As mentioned below, it might be possible to type functions like goto based on the available routes. It probably gets tricky with relative routes, but that could be a bailout.
Typed links
This is a little tricky for us, since we use <a> instead of <Link>, but it would be neat if it was possible to typecheck links somehow.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:31
- Comments:16 (15 by maintainers)
Top Results From Across the Web
Generate GraphQL Types with Apollo Codegen Tutorial
In this post, you'll learn how to use Apollo's GraphQL codegen to generate TypeScript types for GraphQL operations in your Apollo Client ...
Read more >Automatically Generate TypeScript Types For Your ... - YouTube
With StepZen you can build a GraphQL API for any data source in minutes, and deploy it to the cloud in seconds.
Read more >Generate TypeScript Types from GraphQL - Fullstack.io
This post shows you how to generate TypeScript types from your GraphQL server. And it's super handy because not only is your code...
Read more >Generated Types | RedwoodJS Docs
To add to the TypeScript (and JavaScript!) experience, Redwood generates types for you. These generated types not only include your GraphQL operations, but...
Read more >Automatically generate Typescript types ... - DEV Community
Introduction In this post, I will show you how to automatically generate types for your... Tagged with typescript, graphql, javascript, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Originally posted in #3090, here I describe how
goto,fetch(and like @ebeloded mentionedinvalidate,prefetch,prefetchRoutes) could be improved with some type-information:Describe the problem
In larger projects (also in smaller projects) it would be great if the
gotoand thefetchfunctions could offer more typesafety. Problems with missing typesafety are:/costumerinstead of/customerroutesfolder, all links need to be updated toofetchwith the wrong method e.g. usingPUTinstead ofPATCHIt would be great if the
gotoand thefetchfunctions 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
gotoand thefetchfunction. Similar tho the already generated.svelte-kit/dev/generated/manifest.jsfile, SvelteKit could generate ad.tswith types depending on the.sveltefiles inside theroutesfolder and depending on the function inside a.jsand.tsEndpoints file.These types then could be used to enhance the
gotoandfetchfunctions. The typesafe functions could replace the existing import fromapp/navigation. I’m not sure how this could work for thefetchfunction since you don’t really import it from anywhere. Or this could be an additional function you need to import fromapp/typesafeor something similar.Here is a working example how I think this could look like:
gotofunctionfetchfunctionYou can copy these examples to a
.tsfile and try passing some valid/invalid strings to thegotoandfetchfunctions. Lines annotated with// @ts-expect-errorare invalid and will throw a TypeScript Error.