Skip to content

Commit e0d9ff7

Browse files
committed
generated correct specific type for paths.base and paths.assets
1 parent 35574d5 commit e0d9ff7

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ export function write_api(config, manifest_data) {
1818
fs.mkdirSync(types_dir, { recursive: true });
1919
} catch {}
2020

21-
const api_imports = [
22-
'import type * as Kit from "@sveltejs/kit";',
23-
'import type { base } from "$app/paths";'
24-
];
21+
const api_imports = ['import type * as Kit from "@sveltejs/kit";'];
2522

2623
/** @type {string[]} */
2724
const api_declarations = [];
2825

26+
api_declarations.push("declare module '__sveltekit/paths' {");
27+
api_declarations.push(
28+
`\tinterface paths { \n\t\tbase: '${config.kit.paths.base}', \n\t\tassets: '${config.kit.paths.assets}'\n\t}`
29+
);
30+
api_declarations.push('}');
31+
2932
api_declarations.push(
3033
'interface TypedRequestInit<Method extends string> extends RequestInit { \n\tmethod?: Method; \n}'
3134
);
@@ -101,7 +104,7 @@ export function write_api(config, manifest_data) {
101104
api_endpoints.push(`\t\t${index}: { `);
102105
api_endpoints.push(`\t\t\tid: \`${route.id}\`; `);
103106
api_endpoints.push(
104-
`\t\t\tdoes_match: RemoveTrailingSlash<ToCheck> extends \`\${typeof base}${matcher_str}\` ? ${validator_str} : false; `
107+
`\t\t\tdoes_match: RemoveTrailingSlash<ToCheck> extends \`${config.kit.paths.base}${matcher_str}\` ? ${validator_str} : false; `
105108
);
106109
if (route.endpoint) {
107110
const route_import_path = posixify(

packages/kit/src/types/ambient.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,21 @@ declare module '__sveltekit/environment' {
8989

9090
/** Internal version of $app/paths */
9191
declare module '__sveltekit/paths' {
92+
interface paths {}
9293
/**
9394
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths).
9495
*
9596
* Example usage: `<a href="{base}/your-page">Link</a>`
9697
*/
97-
export let base: '' | `/${string}`;
98+
export let base: paths extends { base: infer T } ? T : '' | `/${string}`;
9899
/**
99100
* An absolute path that matches [`config.kit.paths.assets`](https://kit.svelte.dev/docs/configuration#paths).
100101
*
101102
* > If a value for `config.kit.paths.assets` is specified, it will be replaced with `'/_svelte_kit_assets'` during `vite dev` or `vite preview`, since the assets don't yet live at their eventual URL.
102103
*/
103-
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
104+
export let assets: paths extends { assets: infer T }
105+
? T
106+
: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
104107
export let relative: boolean | undefined; // TODO in 2.0, make this a `boolean` that defaults to `true`
105108
export function reset(): void;
106109
export function override(paths: { base: string; assets: string }): void;

0 commit comments

Comments
 (0)