Skip to content

Commit 1f67a4c

Browse files
committed
refactor: ♻️ update definitions
1 parent 7e90e40 commit 1f67a4c

20 files changed

+97
-82
lines changed

definitions/$app.forms.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ action results.
1616
## Example
1717

1818
```svelte
19-
<script>
19+
<script lang="ts">
2020
import { enhance } from '$app/forms';
2121
</script>
2222

definitions/$app.paths.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ route IDs respecting `config.kit.paths`.
1616
## Example
1717

1818
```svelte
19-
<script>
19+
<script lang="ts">
2020
import { asset, resolve } from '$app/paths';
21-
const img = asset('/logo.png');
22-
const url = resolve('/blog/[slug]', { slug: 'hello-world' });
21+
const img: string = asset('/logo.png');
22+
const url: string = resolve('/blog/[slug]', { slug: 'hello-world' });
2323
</script>
2424
2525
<img src={img} alt="logo" />

definitions/$app.state.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Note: `$app/state` values update reactively only via runes (e.g.
2121
## Example
2222

2323
```svelte
24-
<script>
24+
<script lang="ts">
2525
import { page, navigating, updated } from '$app/state';
2626
const path = $derived(page.url.pathname);
2727
</script>

definitions/$app.stores.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ identical APIs via runes). Existing apps may still use `$app/stores`.
1717
## Example
1818

1919
```svelte
20-
<script>
20+
<script lang="ts">
2121
import { page, navigating, updated } from '$app/stores';
2222
$: title = $page.url.pathname;
2323
</script>

definitions/$derived.by.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ computations
1212

1313
## Examples
1414

15-
```js
15+
```ts
1616
// Expensive computation that should be memoized
17-
let items = $state([1, 2, 3, 4, 5]);
18-
let filter = $state('even');
17+
let items: number[] = $state([1, 2, 3, 4, 5]);
18+
let filter: 'even' | 'odd' = $state('even');
1919

2020
const expensiveResult = $derived.by(() => {
2121
console.log('Computing expensive result...');
@@ -47,7 +47,7 @@ const processedData = $derived.by(() => {
4747

4848
// When you need multiple statements
4949
const validation = $derived.by(() => {
50-
const errors = [];
50+
const errors: string[] = [];
5151

5252
if (items.length === 0) {
5353
errors.push('Items cannot be empty');
@@ -75,7 +75,7 @@ async function like() {
7575
}
7676

7777
// Destructuring: each piece remains reactive
78-
let { a, b } = $derived(getPair()); // roughly equivalent to $derived(getPair().a) and ...b
78+
let { a, b } = $derived(getPair()); // each remains reactive
7979

8080
// Dependency control
8181
let total2 = $derived.by(() => {

definitions/$state.md

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,29 @@
11
# $state Definition
22

3-
**Definition:** Creates reactive state that triggers UI updates when
4-
the value changes
5-
**Syntax:** `$state<T>(initial: T): T` |
6-
`$state<T>(): T | undefined`
7-
**Parameters:**
8-
9-
- `initial` - The initial value (optional)
10-
**Returns:** Reactive proxy of the initial value
11-
**Variants:**
12-
- `$state.raw<T>(initial: T): T` - Non-deeply-reactive state
13-
- `$state.snapshot<T>(state: T): Snapshot<T>` - Static snapshot of
14-
reactive state
3+
**Definition:** Declares reactive state. When the value changes, the
4+
UI updates. Arrays and plain objects become deeply reactive proxies;
5+
primitives (like numbers/strings/booleans) remain normal values.
6+
**Also see:** `$state.raw`, `$state.snapshot`
157

168
## Examples
179

18-
```js
10+
```ts
1911
// Basic reactive state
2012
let count = $state(0);
21-
count++; // Triggers reactivity
13+
count++; // triggers an update
2214

23-
// Object state (deeply reactive)
15+
// Object/array state (deeply reactive)
2416
let user = $state({ name: 'Alice', age: 30 });
25-
user.name = 'Bob'; // Triggers reactivity
17+
user.name = 'Bob'; // triggers an update of only the places that read `user.name`
2618

27-
// Optional initial value
19+
// You can declare without an initial value with generics
2820
let data = $state<string>(); // undefined initially
2921
```
3022

3123
## Notes
3224

33-
- Deep reactivity: Arrays and simple objects are proxied deeply, so
34-
property changes and array methods (e.g., `push`) trigger granular
25+
- Deep reactivity: Arrays and simple objects are proxied deeply;
26+
property changes and array methods (e.g. `push`) trigger granular
3527
updates.
3628
- Raw state: Use `$state.raw` to avoid deep reactivity and update only
3729
on reassignment for performance with large data.

definitions/+layout.server.ts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import type { LayoutServerLoad } from './$types';
2222
import { error } from '@sveltejs/kit';
2323

2424
export const load: LayoutServerLoad = async ({ locals }) => {
25-
if (!locals.user) error(401, 'not logged in');
25+
if (!locals.user) throw error(401, 'not logged in');
2626
return { user: locals.user };
2727
};
2828
```

definitions/+page.server.ts.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ export const load: PageServerLoad = async ({
2929
locals,
3030
}) => {
3131
const session = cookies.get('sessionid');
32-
if (!session) redirect(307, '/login');
32+
if (!session) throw redirect(307, '/login');
3333
const user = await locals.db.getUser(session);
34-
if (!user) error(401, 'not logged in');
34+
if (!user) throw error(401, 'not logged in');
3535
return { user };
3636
};
3737

definitions/actions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ to handle POSTed form data and mutations.
2020

2121
```ts
2222
// +page.server.ts
23+
import type { Actions } from './$types';
2324
import { fail, redirect } from '@sveltejs/kit';
2425

25-
export const actions = {
26+
export const actions: Actions = {
2627
login: async ({ request, locals }) => {
2728
const data = await request.formData();
2829
const email = data.get('email');
2930
if (!email) return fail(400, { message: 'Email required' });
3031
await locals.auth.login(email as string);
31-
redirect(303, '/dashboard');
32+
throw redirect(303, '/dashboard');
3233
},
3334
};
3435
```

definitions/cookies.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ requests.
77

88
**API:**
99

10-
- `get(name)` — read a cookie
11-
- `set(name, value, options)` — set cookie
12-
- `delete(name, options)` — delete cookie
10+
- `get(name)` — read a cookie (optionally `getAll()`)
11+
- `set(name, value, { path, ...options })` — set cookie (path is
12+
required)
13+
- `delete(name, { path, ...options })` — delete cookie (path must
14+
match)
1315

1416
**Returns:** Cookie helpers bound to the current request.
1517

@@ -18,6 +20,7 @@ requests.
1820
```ts
1921
export const load = ({ cookies }) => {
2022
const theme = cookies.get('theme') ?? 'light';
23+
cookies.set('seen', '1', { path: '/', httpOnly: true });
2124
return { theme };
2225
};
2326
```
@@ -28,5 +31,5 @@ export const load = ({ cookies }) => {
2831

2932
Notes:
3033

31-
- `event.fetch` forwards cookies for same-origin and certain
32-
subdomains; otherwise add them in `handleFetch`.
34+
- `event.fetch` forwards cookies for same-origin requests; to forward
35+
to other origins, add headers in `handleFetch`.

0 commit comments

Comments
 (0)