Skip to content

Commit

Permalink
generated correct specific type for paths.base and paths.assets
Browse files Browse the repository at this point in the history
  • Loading branch information
difanta committed Jul 26, 2023
1 parent 9a7345b commit 00c4b11
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
13 changes: 8 additions & 5 deletions packages/kit/src/core/sync/write_types/write_api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ export function write_api(config, manifest_data) {
fs.mkdirSync(types_dir, { recursive: true });
} catch {}

const api_imports = [
'import type * as Kit from "@sveltejs/kit";',
'import type { base } from "$app/paths";'
];
const api_imports = ['import type * as Kit from "@sveltejs/kit";'];

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

api_declarations.push("declare module '__sveltekit/paths' {");
api_declarations.push(
`\tinterface paths { \n\t\tbase: '${config.kit.paths.base}', \n\t\tassets: '${config.kit.paths.assets}'\n\t}`
);
api_declarations.push('}');

api_declarations.push(
'interface TypedRequestInit<Method extends string> extends RequestInit { \n\tmethod?: Method; \n}'
);
Expand Down Expand Up @@ -101,7 +104,7 @@ export function write_api(config, manifest_data) {
api_endpoints.push(`\t\t${index}: { `);
api_endpoints.push(`\t\t\tid: \`${route.id}\`; `);
api_endpoints.push(
`\t\t\tdoes_match: RemoveTrailingSlash<ToCheck> extends \`\${typeof base}${matcher_str}\` ? ${validator_str} : false; `
`\t\t\tdoes_match: RemoveTrailingSlash<ToCheck> extends \`${config.kit.paths.base}${matcher_str}\` ? ${validator_str} : false; `
);
if (route.endpoint) {
const route_import_path = posixify(
Expand Down
7 changes: 5 additions & 2 deletions packages/kit/src/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,21 @@ declare module '__sveltekit/environment' {

/** Internal version of $app/paths */
declare module '__sveltekit/paths' {
interface paths {}
/**
* A string that matches [`config.kit.paths.base`](https://kit.svelte.dev/docs/configuration#paths).
*
* Example usage: `<a href="{base}/your-page">Link</a>`
*/
export let base: '' | `/${string}`;
export let base: paths extends { base: infer T } ? T : '' | `/${string}`;
/**
* An absolute path that matches [`config.kit.paths.assets`](https://kit.svelte.dev/docs/configuration#paths).
*
* > 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.
*/
export let assets: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
export let assets: paths extends { assets: infer T }
? T
: '' | `https://${string}` | `http://${string}` | '/_svelte_kit_assets';
export let relative: boolean | undefined; // TODO in 2.0, make this a `boolean` that defaults to `true`
export function reset(): void;
export function override(paths: { base: string; assets: string }): void;
Expand Down

0 comments on commit 00c4b11

Please sign in to comment.