Skip to content

Commit 20ed756

Browse files
Use satifies operator (#7861)
* Use satifies operator * generate TS with `satisfies` (#8001) Co-authored-by: Rich Harris <[email protected]>
1 parent 4d2b811 commit 20ed756

File tree

9 files changed

+41
-13
lines changed

9 files changed

+41
-13
lines changed

.changeset/cool-seahorses-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-svelte': patch
3+
---
4+
5+
Use `satisfies` operator

documentation/docs/20-core-concepts/20-load.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export async function load({ cookies }) {
271271
Both server-only and shared `load` functions have access to a `setHeaders` function that, when running on the server, can set headers for the response. (When running in the browser, `setHeaders` has no effect.) This is useful if you want the page to be cached, for example:
272272

273273
```js
274-
// @errors: 2322
274+
// @errors: 2322 1360
275275
/// file: src/routes/products/+page.js
276276
/** @type {import('./$types').PageLoad} */
277277
export async function load({ fetch, setHeaders }) {

documentation/docs/30-advanced/20-hooks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ declare namespace App {
158158

159159
```js
160160
/// file: src/hooks.server.js
161-
// @errors: 2322 2571 2339
161+
// @errors: 2322 1360 2571 2339
162162
// @filename: ambient.d.ts
163163
const Sentry: any;
164164

@@ -178,7 +178,7 @@ export function handleError({ error, event }) {
178178
179179
```js
180180
/// file: src/hooks.client.js
181-
// @errors: 2322 2571 2339
181+
// @errors: 2322 1360 2571 2339
182182
// @filename: ambient.d.ts
183183
const Sentry: any;
184184

documentation/docs/30-advanced/25-errors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Unexpected errors will go through the [`handleError`](/docs/hooks#shared-hooks-h
8181

8282
```js
8383
/// file: src/hooks.server.js
84-
// @errors: 2322 2571 2339
84+
// @errors: 2322 1360 2571 2339
8585
// @filename: ambient.d.ts
8686
const Sentry: any;
8787

documentation/docs/50-reference/40-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The `RequestHandler` and `Load` types both accept a `Params` argument allowing y
2020

2121
```js
2222
/// file: src/routes/[foo]/[bar]/[baz]/+page.server.js
23-
// @errors: 2355 2322
23+
// @errors: 2355 2322 1360
2424
/** @type {import('@sveltejs/kit').RequestHandler<{
2525
* foo: string;
2626
* bar: string;

packages/create-svelte/templates/default/src/routes/sverdle/+page.server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Game } from './game';
33
import type { PageServerLoad, Actions } from './$types';
44

55
/** @type {import('./$types').PageServerLoad} */
6-
export const load: PageServerLoad = ({ cookies }) => {
6+
export const load = (({ cookies }) => {
77
const game = new Game(cookies.get('sverdle'));
88

99
return {
@@ -23,10 +23,10 @@ export const load: PageServerLoad = ({ cookies }) => {
2323
*/
2424
answer: game.answers.length >= 6 ? game.answer : null
2525
};
26-
};
26+
}) satisfies PageServerLoad;
2727

2828
/** @type {import('./$types').Actions} */
29-
export const actions: Actions = {
29+
export const actions = {
3030
/**
3131
* Modify game state in reaction to a keypress. If client-side JavaScript
3232
* is available, this will happen in the browser instead of here
@@ -68,4 +68,4 @@ export const actions: Actions = {
6868
restart: async ({ cookies }) => {
6969
cookies.delete('sverdle');
7070
}
71-
};
71+
} satisfies Actions;

packages/kit/src/core/sync/write_types/index.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,26 @@ test('Rewrites action types for a TypeScript module', () => {
265265
);
266266
});
267267

268+
test('Leaves satisfies operator untouched', () => {
269+
const source = `
270+
import type { Actions, PageServerLoad, RequestEvent } from './$types';
271+
export function load({ params }) {
272+
return {
273+
a: 1
274+
};
275+
} satisfies PageServerLoad
276+
export const actions = {
277+
a: () => {},
278+
b: (param: RequestEvent) => {},
279+
c: (param) => {},
280+
} satisfies Actions
281+
`;
282+
283+
const rewritten = tweak_types(source, true);
284+
285+
assert.equal(rewritten?.exports, ['load', 'actions']);
286+
assert.equal(rewritten?.modified, false);
287+
assert.equal(rewritten?.code, source);
288+
});
289+
268290
test.run();

sites/kit.svelte.dev/src/lib/docs/server/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,12 @@ function convert_to_ts(js_code, indent = '', offset = '') {
469469
code.overwrite(
470470
node.getStart(),
471471
node.name.getEnd(),
472-
`${is_export ? 'export ' : ''}const ${node.name.getText()}: ${name} = ${
472+
`${is_export ? 'export ' : ''}const ${node.name.getText()} = (${
473473
is_async ? 'async ' : ''
474474
}`
475475
);
476476
code.appendLeft(node.body.getStart(), '=> ');
477+
code.appendLeft(node.body.getEnd(), `) satisfies ${name};`);
477478

478479
modified = true;
479480
} else if (

sites/kit.svelte.dev/src/lib/docs/server/index.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,18 @@ export function GET(event) {
5959
\`\`\`generated-ts
6060
// @errors: 2461
6161
/// file: src/routes/what-is-my-user-agent/+server.ts
62-
import type { RequestHandler } from './$types';
6362
import { json } from '@sveltejs/kit';
63+
import type { RequestHandler } from './$types';
6464
65-
export const GET: RequestHandler = (event) => {
65+
export const GET = ((event) => {
6666
// log all headers
6767
console.log(...event.request.headers);
6868
6969
return json({
7070
// retrieve a specific header
7171
userAgent: event.request.headers.get('user-agent')
7272
});
73-
}
73+
}) satisfies RequestHandler;
7474
\`\`\`
7575
7676
etc etc

0 commit comments

Comments
 (0)