Skip to content

Commit a8cc450

Browse files
Rich-Harriselliott-with-the-longest-name-on-githubeltigerchino
authored
feat: typed route ids (#13864)
* add `RouteId` to `$app/types` * more * create directory * hmm * fix * changeset * regenerate * some basic docs * oh wow that seemed to work * test * ouch * oh actually lets do this instead * fix * couple more, though still doesnt quite fix everything * forgive me * fix * fix * make url.pathname type safe as well * oops didnt mean to commit that * fix * page.url.pathname needs to account for base path * resolveRoute -> resolve * add `asset(...)` function, deprecate `base` and `assets` * fix * changesets * regenerate * Update packages/kit/src/core/sync/write_types/test/app-types/+page.ts Co-authored-by: Rich Harris <[email protected]> * Update packages/kit/src/core/sync/write_types/test/app-types/+page.ts Co-authored-by: Rich Harris <[email protected]> * chore: extract regexes into named functions * Update documentation/docs/98-reference/20-$app-types.md * Update documentation/docs/98-reference/20-$app-types.md Co-authored-by: Rich Harris <[email protected]> * empty * add an example * silence error * fix typo * link from deprecation notices * since --------- Co-authored-by: Elliott Johnson <[email protected]> Co-authored-by: Tee Ming <[email protected]>
1 parent a18e21a commit a8cc450

File tree

21 files changed

+408
-91
lines changed

21 files changed

+408
-91
lines changed

.changeset/proud-rules-bow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: better type-safety for `page.route.id`, `page.params`, page.url.pathname` and various other places

.changeset/slow-weeks-wave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: `resolve(...)` and `asset(...)` helpers for resolving paths

.changeset/wicked-bananas-yawn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': minor
3+
---
4+
5+
feat: Add `$app/types` module with `Asset`, `RouteId`, `Pathname`, `ResolvedPathname` `RouteParams<T>` and `LayoutParams<T>`
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
title: $app/types
3+
---
4+
5+
This module contains generated types for the routes in your app.
6+
7+
<blockquote class="since note">
8+
<p>Available since 2.26</p>
9+
</blockquote>
10+
11+
```js
12+
// @noErrors
13+
import type { RouteId, RouteParams, LayoutParams } from '$app/types';
14+
```
15+
16+
## Asset
17+
18+
A union of all the filenames of assets contained in your `static` directory.
19+
20+
<div class="ts-block">
21+
22+
```dts
23+
type Asset = '/favicon.png' | '/robots.txt';
24+
```
25+
26+
</div>
27+
28+
## RouteId
29+
30+
A union of all the route IDs in your app. Used for `page.route.id` and `event.route.id`.
31+
32+
<div class="ts-block">
33+
34+
```dts
35+
type RouteId = '/' | '/my-route' | '/my-other-route/[param]';
36+
```
37+
38+
</div>
39+
40+
## Pathname
41+
42+
A union of all valid pathnames in your app.
43+
44+
<div class="ts-block">
45+
46+
```dts
47+
type Pathname = '/' | '/my-route' | `/my-other-route/${string}` & {};
48+
```
49+
50+
</div>
51+
52+
## ResolvedPathname
53+
54+
`Pathname`, but possibly prefixed with a [base path](https://svelte.dev/docs/kit/configuration#paths). Used for `page.url.pathname`.
55+
56+
<div class="ts-block">
57+
58+
```dts
59+
type Pathname = `${'' | `/${string}`}/` | `${'' | `/${string}`}/my-route` | `${'' | `/${string}`}/my-other-route/${string}` | {};
60+
```
61+
62+
</div>
63+
64+
## RouteParams
65+
66+
A utility for getting the parameters associated with a given route.
67+
68+
```ts
69+
// @errors: 2552
70+
type BlogParams = RouteParams<'/blog/[slug]'>; // { slug: string }
71+
```
72+
73+
<div class="ts-block">
74+
75+
```dts
76+
type RouteParams<T extends RouteId> = { /* generated */ } | Record<string, never>;
77+
```
78+
79+
</div>
80+
81+
## LayoutParams
82+
83+
A utility for getting the parameters associated with a given layout, which is similar to `RouteParams` but also includes optional parameters for any child route.
84+
85+
<div class="ts-block">
86+
87+
```dts
88+
type RouteParams<T extends RouteId> = { /* generated */ } | Record<string, never>;
89+
```
90+
91+
</div>

packages/adapter-auto/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"target": "es2022",
88
"module": "node16",
99
"moduleResolution": "node16",
10-
"baseUrl": "."
10+
"baseUrl": ".",
11+
"skipLibCheck": true
1112
},
1213
"include": ["**/*.js"]
1314
}

packages/adapter-node/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"baseUrl": ".",
1212
"paths": {
1313
"@sveltejs/kit": ["../kit/types/index"]
14-
}
14+
},
15+
"skipLibCheck": true
1516
},
1617
"include": ["index.js", "src/**/*.js", "tests/**/*.js", "internal.d.ts", "utils.js"],
1718
"exclude": ["tests/smoke.spec_disabled.js"]

packages/adapter-static/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"baseUrl": ".",
1212
"paths": {
1313
"@sveltejs/kit": ["../kit/types/index"]
14-
}
14+
},
15+
"skipLibCheck": true
1516
},
1617
"include": ["index.js", "test/utils.js"]
1718
}

packages/adapter-vercel/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"baseUrl": ".",
1313
"paths": {
1414
"@sveltejs/kit": ["../kit/types/index"]
15-
}
15+
},
16+
"skipLibCheck": true
1617
},
1718
"include": ["*.js", "files/**/*.js", "internal.d.ts", "test/**/*.js"]
1819
}

packages/kit/scripts/generate-dts.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createBundle } from 'dts-buddy';
2-
import { readFileSync } from 'node:fs';
2+
import { readFileSync, writeFileSync } from 'node:fs';
33

44
await createBundle({
55
output: 'types/index.d.ts',
@@ -28,3 +28,9 @@ if (types.includes('__sveltekit/')) {
2828
types
2929
);
3030
}
31+
32+
// this is hacky as all hell but it gets the tests passing. might be a bug in dts-buddy?
33+
// prettier-ignore
34+
writeFileSync('./types/index.d.ts', types.replace("declare module '$app/server' {", `declare module '$app/server' {
35+
// @ts-ignore
36+
import { LayoutParams as AppLayoutParams, RouteId as AppRouteId } from '$app/types'`));

packages/kit/src/core/sync/write_tsconfig.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ export function get_tsconfig(kit) {
9898
const config = {
9999
compilerOptions: {
100100
// generated options
101-
paths: get_tsconfig_paths(kit),
101+
paths: {
102+
...get_tsconfig_paths(kit),
103+
'$app/types': ['./types/index.d.ts']
104+
},
102105
rootDirs: [config_relative('.'), './types'],
103106

104107
// essential options

0 commit comments

Comments
 (0)