Skip to content
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

write generated types to __types directories #4705

Merged
merged 10 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smart-nails-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'create-svelte': patch
---

import generated types from `__types/index.d.ts` file
5 changes: 5 additions & 0 deletions .changeset/soft-months-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] write generated types to `__types` directories
8 changes: 4 additions & 4 deletions documentation/docs/15-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Needless to say, this is cumbersome to write out, and less portable (if you were
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
/// file: .svelte-kit/types/src/routes/[foo]/[bar]/__types/[baz].d.ts
/// link: false
import type { RequestHandler as GenericRequestHandler, Load as GenericLoad } from '@sveltejs/kit';

Expand All @@ -45,7 +45,7 @@ These files can be imported into your endpoints and pages as siblings, thanks to

```js
/// file: src/routes/[foo]/[bar]/[baz].js
// @filename: [baz].d.ts
// @filename: __types/[baz].d.ts
import type { RequestHandler as GenericRequestHandler, Load as GenericLoad } from '@sveltejs/kit';

export type RequestHandler<Body = any> = GenericRequestHandler<
Expand All @@ -56,15 +56,15 @@ export type RequestHandler<Body = any> = GenericRequestHandler<
// @filename: index.js
// @errors: 2355
// ---cut---
/** @type {import('./[baz]').RequestHandler} */
/** @type {import('./__types/[baz]').RequestHandler} */
export async function get({ params }) {
// ...
}
```

```svelte
<script context="module">
/** @type {import('./[baz]').Load} */
/** @type {import('./__types/[baz]').Load} */
export async function load({ params, fetch, session, stuff }) {
// ...
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { api } from './_api';
import type { RequestHandler } from './index';
import type { RequestHandler } from './__types';

/** @type {import('./index').RequestHandler} */
/** @type {import('./__types').RequestHandler} */
export const get: RequestHandler = async ({ locals }) => {
// locals.userid comes from src/hooks.js
const response = await api('get', `todos/${locals.userid}`);
Expand Down
8 changes: 7 additions & 1 deletion packages/kit/src/core/sync/write_types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { rimraf } from '../../utils/filesystem.js';
import { parse_route_id } from '../../utils/routing.js';
import { write_if_changed } from './utils.js';

Expand All @@ -22,6 +23,8 @@ export type Load<
* @param {import('types').ManifestData} manifest_data
*/
export function write_types(config, manifest_data) {
rimraf(`${config.kit.outDir}/types`);

/** @type {Map<string, { params: string[], type: 'page' | 'endpoint' | 'both' }>} */
const shadow_types = new Map();

Expand Down Expand Up @@ -70,8 +73,11 @@ export function write_types(config, manifest_data) {

content.unshift(header(imports.join(', ')));

const parts = (key || 'index').split('/');
parts.push('__types', /** @type {string} */ (parts.pop()));

write_if_changed(
`${config.kit.outDir}/types/${key || 'index'}.d.ts`,
`${config.kit.outDir}/types/${parts.join('/')}.d.ts`,
content.join('\n').trim()
);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
/**
* @type {import('./foo').Foo}
*/
export let foo;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These also seem unrelated, but possibly ok to keep because else prettier complains (?)

</script>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
/** @type {import('./[slug]').Load} */
/** @type {import('./__types/[slug]').Load} */
export function load({ url, params }) {
return {
props: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('./a.json').RequestHandler} */
/** @type {import('./__types/a.json').RequestHandler} */
export function get({ url }) {
const answer = url.searchParams.get('answer') || '42';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('./b.json').RequestHandler} */
/** @type {import('./__types/b.json').RequestHandler} */
export function get({ request }) {
const cookie = request.headers.get('cookie');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('./[x]').RequestHandler} */
/** @type {import('./__types/[x]').RequestHandler} */
export function get({ locals }) {
return {
body: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
/** @type {import('./[x]').Load} */
/** @type {import('./__types/[x]').Load} */
export function load({ routeId }) {
return {
props: { routeId }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
/** @type {import('./fetch-404').Load} */
/** @type {import('./__types/fetch-404').Load} */
export async function load({ fetch }) {
const { status } = await fetch('/missing.json');

Expand Down