Skip to content

Route params incorrectly typed in $app/state #15471

@Stadly

Description

@Stadly

Describe the bug

I have this locale param matcher:

import type { ParamMatcher } from "@sveltejs/kit";

const locales = ["en", "nb"] as const;

export const match = ((value): value is (typeof locales)[number] => {
  return locales.includes(value);
}) satisfies ParamMatcher;

When looking at .svelte-kit/types/src/routes/[[locale=locale]]/$types.d.ts, I see this:

type MatcherParam<M> = M extends (param : string) => param is infer U ? U extends string ? U : string : string;
type RouteParams = { locale?: MatcherParam<typeof import('../../../../../src/params/locale.js').match> };

But in .svelte-kit/non-ambient.ts, it looks like this:

declare module "$app/types" {
  export interface AppTypes {
    RouteParams(): {
      "/[[locale=locale]]": { locale?: string }
    };
  };
}

As a result, in src/routes/[[locale=locale]]/+page.svelte, the params prop is typed correctly, but not the page.params from $app/state.

<script lang="ts">
  import type { PageProps } from "./$types.js";
  import { page } from "$app/state";

  let { params }: PageProps = $props();

  params.locale; // "en" | "no" | undefined
  page.params.locale; // string | undefined
</script>

For the same reason, the params are also incorrectly typed in hooks.

I think non-ambient.ts should use the same approach as $types.d.ts to provide correct types for route params.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-f9pqbngm?file=src%2Froutes%2F%5B%5Blocale%3Dlocale%5D%5D%2F%2Bpage.svelte

Logs

System Info

N/A

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions