Skip to content

feat: narrow page param types using matchers in $app/types (non-ambient.d.ts)#15502

Merged
teemingc merged 9 commits intosveltejs:mainfrom
Stadly:fix/narrow-param-types-to-matchers
Mar 11, 2026
Merged

feat: narrow page param types using matchers in $app/types (non-ambient.d.ts)#15502
teemingc merged 9 commits intosveltejs:mainfrom
Stadly:fix/narrow-param-types-to-matchers

Conversation

@Stadly
Copy link
Contributor

@Stadly Stadly commented Mar 6, 2026

closes #15471

Without this PR, page and layout params are always typed as string in $app/types, $app/state, and hooks, even when having matchers narrowing the type.

If we have this matcher and a route like src/routes/fruits/[page=fruit]:

// src/params/fruit.ts
import type { ParamMatcher } from '@sveltejs/kit';

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

The parameter will now be typed as 'apple' | 'orange' instead of string in $app/types.

Note that this is similar to what we already have when working with page and layout params in src/routes/fruits/[page=fruit]/$types.js and in load functions. The exception is that in load functions and $types.js we know the exact route. In $app/state and hooks we don't so all parameters will be optional and their type a union of the type of all parameters with the same name.


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot
Copy link

changeset-bot bot commented Mar 6, 2026

🦋 Changeset detected

Latest commit: bebc152

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still going through, here are a few other comments

@Stadly Stadly force-pushed the fix/narrow-param-types-to-matchers branch from db35574 to bcab3d4 Compare March 6, 2026 23:41
@Stadly Stadly force-pushed the fix/narrow-param-types-to-matchers branch from bcab3d4 to cadf6de Compare March 6, 2026 23:50
@elliott-with-the-longest-name-on-github
Copy link
Contributor

Pushed some refactors for performance (some of the stuff we were doing was O(n^2)) and to get rid of the ts-ignores we had in there

Should be good to go now, but I'll let another maintainer take a look at it since I changed a bunch of stuff

Thank you!

@svelte-docs-bot
Copy link

@Stadly Stadly changed the title fix: narrow page param types using matchers in $app/types (non-ambient.d.ts) feat: narrow page param types using matchers in $app/types (non-ambient.d.ts) Mar 7, 2026
Copy link
Member

@teemingc teemingc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just added a few comments to suggest making the if/elses easier to read

@Stadly
Copy link
Contributor Author

Stadly commented Mar 9, 2026

Thanks, @teemingc! I think you're best equipped to respond to the comments, @elliott-with-the-longest-name-on-github, but let me know if you want me to do anything :)

@elliott-with-the-longest-name-on-github
Copy link
Contributor

@teemingc thank you! Should be addressed now

Copy link
Member

@teemingc teemingc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@teemingc teemingc merged commit 0f321aa into sveltejs:main Mar 11, 2026
25 checks passed
This was referenced Mar 11, 2026
@teemingc teemingc linked an issue Mar 13, 2026 that may be closed by this pull request
dlrobson added a commit to dlrobson/dlrobson.github.io that referenced this pull request Mar 22, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [@sveltejs/kit](https://svelte.dev) ([source](https://github.com/sveltejs/kit/tree/HEAD/packages/kit)) | [`2.53.4` → `2.55.0`](https://renovatebot.com/diffs/npm/@sveltejs%2fkit/2.53.4/2.55.0) | ![age](https://developer.mend.io/api/mc/badges/age/npm/@sveltejs%2fkit/2.55.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@sveltejs%2fkit/2.53.4/2.55.0?slim=true) |

---

### Release Notes

<details>
<summary>sveltejs/kit (@&#8203;sveltejs/kit)</summary>

### [`v2.55.0`](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2550)

[Compare Source](https://github.com/sveltejs/kit/compare/@sveltejs/kit@2.54.0...@sveltejs/kit@2.55.0)

##### Minor Changes

- feat: page and layout params with matchers are now type narrowed in `$app/types`, leading to better type safety when working with params in `$app/types`, `$app/state`, and hooks. ([#&#8203;15502](sveltejs/kit#15502))

### [`v2.54.0`](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#2540)

[Compare Source](https://github.com/sveltejs/kit/compare/@sveltejs/kit@2.53.4...@sveltejs/kit@2.54.0)

##### Minor Changes

- feat: allow error boundaries to catch errors on the server ([#&#8203;15308](sveltejs/kit#15308))

##### Patch Changes

- chore: upgrade `devalue` ([#&#8203;15535](sveltejs/kit#15535))

- fix: don't wait for remote functions that are not awaited in the template ([#&#8203;15280](sveltejs/kit#15280))

- feat: allow `resolve()` to accept pathnames with a search string and/or hash ([#&#8203;15458](sveltejs/kit#15458))

- chore: remove deprecation warnings for `config.kit.files.*` options when validating the Svelte config file ([#&#8203;15482](sveltejs/kit#15482))

- fix: handles form target attribute in remote form redirects ([#&#8203;15457](sveltejs/kit#15457))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these updates again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My44NC4yIiwidXBkYXRlZEluVmVyIjoiNDMuODQuMiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Reviewed-on: https://forgejo.tail5a98.ts.net/gooilers123/monolith/pulls/45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Route params incorrectly typed in $app/state Use matcher type in AppTypes (e.g. resolve())

3 participants