-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Generated types #4120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generated types #4120
Changes from all commits
d834163
8d20d50
750d63a
9fd6c8c
ea68759
94426cd
d272053
1fdb083
31e1bf0
eb7492f
58ad45a
29c6bc3
a0bb057
36d8b4d
4471a8b
69e7ac0
536634c
282d01c
5410409
a8d6425
59138d0
91e1a66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/kit': patch | ||
| --- | ||
|
|
||
| Generate types for each page/endpoint |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,74 @@ title: Types | |
| --- | ||
|
|
||
| **TYPES** | ||
|
|
||
| ### Generated types | ||
|
|
||
| The [`RequestHandler`](#sveltejs-kit-requesthandler) and [`Load`](#sveltejs-kit-load) types both accept a `Params` argument allowing you to type the `params` object. For example this endpoint expects `foo`, `bar` and `baz` params: | ||
|
|
||
| ```js | ||
| /// file: src/routes/[foo]/[bar]/[baz].js | ||
| // @errors: 2355 | ||
| /** @type {import('@sveltejs/kit').RequestHandler<{ | ||
| * foo: string; | ||
| * bar: string; | ||
| * baz: string | ||
| * }>} */ | ||
| export async function get({ params }) { | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| Needless to say, this is cumbersome to write out, and less portable (if you were to rename the `[foo]` directory to `[qux]`, the type would no longer reflect reality). | ||
|
|
||
| To solve this problem, SvelteKit generates `.d.ts` files for each of your endpoints and pages: | ||
|
|
||
| ```ts | ||
| /// file: .svelte-kit/types/src/routes/[foo]/[bar]/[baz].d.ts | ||
| /// link: false | ||
| import type { RequestHandler as GenericRequestHandler, Load as GenericLoad } from '@sveltejs/kit'; | ||
|
|
||
| export type RequestHandler<Body = any> = GenericRequestHandler< | ||
| { foo: string; bar: string; baz: string }, | ||
| Body | ||
| >; | ||
|
|
||
| export type Load<Props = Record<string, any>> = GenericLoad< | ||
| { foo: string; bar: string; baz: string }, | ||
| Props | ||
| >; | ||
| ``` | ||
|
|
||
| These files can be imported into your endpoints and pages as siblings, thanks to the [`rootDirs`](https://www.typescriptlang.org/tsconfig#rootDirs) option in your TypeScript configuration: | ||
|
|
||
| ```js | ||
| /// file: src/routes/[foo]/[bar]/[baz].js | ||
| // @filename: [baz].d.ts | ||
| import type { RequestHandler as GenericRequestHandler, Load as GenericLoad } from '@sveltejs/kit'; | ||
|
|
||
| export type RequestHandler<Body = any> = GenericRequestHandler< | ||
| { foo: string, bar: string, baz: string }, | ||
| Body | ||
| >; | ||
|
|
||
| // @filename: index.js | ||
| // @errors: 2355 | ||
| // ---cut--- | ||
| /** @type {import('./[baz]').RequestHandler} */ | ||
| export async function get({ params }) { | ||
| // ... | ||
| } | ||
| ``` | ||
|
|
||
| ```svelte | ||
| <script context="module"> | ||
| /** @type {import('./[baz]').Load} */ | ||
| export async function load({ params, fetch, session, stuff }) { | ||
| // ... | ||
| } | ||
| </script> | ||
| ``` | ||
|
|
||
| > For this to work, your own `tsconfig.json` or `jsconfig.json` should extend from the generated `.svelte-kit/tsconfig.json`: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I guess we are mentioning .svelte-kit here. I dunno. It is officially part of the API now that we want people to extend the config in it. I'm not sure whether that changes my opinion about mentioning it up higher instead of calling it a hidden folder.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add a note that that folder is not available immediately, so you'll need to run a command first (we also need to restart the language-server for it to resolve the extended tsconfig again, adding it to this list https://github.com/sveltejs/language-tools/blob/master/packages/svelte-vscode/src/extension.ts#L144).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just checked, they do. Will add a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually i'll do that in a separate PR, because there might be some bikeshedding involved |
||
| > | ||
| > { "extends": ".svelte-kit/tsconfig.json" } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,3 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "moduleResolution": "node", | ||
| "module": "es2020", | ||
| "lib": ["es2020"], | ||
| "target": "es2020", | ||
| /** | ||
| svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript | ||
| to enforce using \`import type\` instead of \`import\` for Types. | ||
| */ | ||
| "importsNotUsedAsValues": "error", | ||
| "isolatedModules": true, | ||
| "resolveJsonModule": true, | ||
| /** | ||
| To have warnings/errors of the Svelte compiler at the correct position, | ||
| enable source maps by default. | ||
| */ | ||
| "sourceMap": true, | ||
| "esModuleInterop": true, | ||
| "skipLibCheck": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "baseUrl": ".", | ||
| "allowJs": true, | ||
| "checkJs": true, | ||
| "paths": { | ||
| "$lib/*": ["src/lib/*"] | ||
| } | ||
| }, | ||
| "include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte"] | ||
| "extends": "./.svelte-kit/tsconfig.json" | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.