Skip to content

docs: update pattern matcher type #11934

@hyunbinseo

Description

@hyunbinseo

Describe the bug

Current documentation types the match as follows:

// src/params/fruit.ts

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

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

This does not narrow the type beyond string.

// src/routes/archive/[page=fruit]/+page.ts

export const load = ({ params: { page } }) => {
  page; // string
};

To narrow the type - which was implemented in the following PR - the match type should be updated:

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

export const match = ((param): param is 'apple' | 'orange' => {
  return param === 'apple' || param === 'orange';
}) satisfies ParamMatcher;
export const load = ({ params: { page } }) => {
  page; // "apple" | "orange"
};

Reproduction

Reference the example code above.

Logs

No response

System Info

System:
  OS: macOS 14.2.1
  CPU: (10) arm64 Apple M1 Pro
  Memory: 116.55 MB / 16.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 20.11.1 - ~/.volta/tools/image/node/20.11.1/bin/node
  npm: 10.2.4 - ~/.volta/tools/image/node/20.11.1/bin/npm
  pnpm: 8.15.4 - ~/.volta/bin/pnpm
Browsers:
  Chrome: 122.0.6261.94
  Edge: 122.0.2365.63
  Safari: 17.2.1
npmPackages:
  @sveltejs/adapter-auto: ^3.0.0 => 3.1.1 
  @sveltejs/kit: ^2.0.0 => 2.5.2 
  @sveltejs/vite-plugin-svelte: ^3.0.0 => 3.0.2 
  svelte: ^4.2.7 => 4.2.12 
  vite: ^5.0.3 => 5.1.5

Severity

annoyance

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions