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