Skip to content

Matcher type in PathnameΒ #14051

@WAKayser

Description

@WAKayser

Describe the problem

I have a site with a optional matcher for a language id at the start of the path.. This allows me to translate into a predefined set of languages. But the type for Pathname just resolves to string at that url part. So for the / path this add | `${string}` & {}; to the type. Which causes any string to be accepted for pathname.

For the accepted languages I have a well defined list of ids. Similar to the matching example in the docs: https://svelte.dev/docs/kit/advanced-routing#Matching.

Describe the proposed solution

Ideally we could figure out the type and use that instead of string.

This could be a type exported from the matcher file. For example:

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

export type Param = 'apple' | 'orange';

export const match = ((param: string): param is Param => {
	return param === 'apple' || param === 'orange';
}) satisfies ParamMatcher;

this is the more explicit version of defining the Param. but it can also be done by inspecting the match.

type ExtractPredicateType<T> = T extends (param: string) => param is infer U ? U: never;

export const match = ((param: string): param is (typeof locales)[number] => {
	return isValidLocale(param);
}) satisfies ParamMatcher;

type expectedValues = ExtractPredicateType<typeof match>;

Alternatives considered

No response

Importance

nice to have

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