From b6ba0b2ae35661d64103a8114ca13c7eacba5b95 Mon Sep 17 00:00:00 2001 From: Jared Dunham Date: Wed, 1 Jul 2026 19:47:56 -0500 Subject: [PATCH] Added generic type parameter example for $state --- documentation/docs/07-misc/03-typescript.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/documentation/docs/07-misc/03-typescript.md b/documentation/docs/07-misc/03-typescript.md index b7f49f9c6f..daa73b5e98 100644 --- a/documentation/docs/07-misc/03-typescript.md +++ b/documentation/docs/07-misc/03-typescript.md @@ -170,6 +170,12 @@ If you don't give `$state` an initial value, part of its types will be `undefine let count: number = $state(); ``` +You can pass the type directly as a generic parameter to safely handle this. TypeScript will infer the variable as `number | undefined`. + +```ts +let count = $state(); +``` + If you know that the variable _will_ be defined before you first use it, use an `as` casting. This is especially useful in the context of classes: ```ts