Skip to content

docs: added generic type parameter example for $state#18494

Open
dunhamjared wants to merge 1 commit into
sveltejs:mainfrom
dunhamjared:patch-1
Open

docs: added generic type parameter example for $state#18494
dunhamjared wants to merge 1 commit into
sveltejs:mainfrom
dunhamjared:patch-1

Conversation

@dunhamjared

Copy link
Copy Markdown

Overview

Added an example demonstrating how to pass a generic type parameter to $state when an initial value is omitted.

Why

The current documentation outlines how a missing initial value results in an undefined type error. It then provides a solution using type assertion (as) for class constructors.

While the assertion is perfect for guaranteed assignments, building DOM bindings or components with delayed mounting need a safe way to type reactive variables as Type | undefined.

So adding a quick example of $state<Type>() bridges this gap and provides a complete picture of how to handle types without initial values.

@pkg-svelte-dev

pkg-svelte-dev Bot commented Jul 2, 2026

Copy link
Copy Markdown

Install the latest version of svelte from b6ba0b2:

pnpm add https://pkg.svelte.dev/svelte/c/b6ba0b2ae35661d64103a8114ca13c7eacba5b95

Open in pkg.svelte.dev: https://pkg.svelte.dev/svelte/c/b6ba0b2ae35661d64103a8114ca13c7eacba5b95

Note

This PR is from a fork. A maintainer must approve the fork build on pkg.svelte.dev before the install URLs above will work.

@changeset-bot

changeset-bot Bot commented Jul 2, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: b6ba0b2

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant