ITADN

False TS2367 error: $state with explicit type annotation infers initializer literal type instead of declared type

#18231Closedcdcarson 创建于 2026-05-16
C
cdcarsoncommented
### Describe the bug I'm getting a TS2367 error in both the editor and svelte check from the following: ```svelte <script lang='ts'> const colors = [ 'red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet', 'black', 'white', ] as const; type Color = (typeof colors)[number]; let favorite: Color = $state('red'); // the following errors.... let isMonochrome: boolean = $derived( favorite === 'black' || favorite === 'white', ); // but this does not... let isMonochrome2: boolean = $derived( ['black', 'white', 'gray'].includes(favorite), ); </script> ``` From the fact that`isMonochrome` errors and `isMonochrome2` does not, I think the `let favorite: Color = $state('red');` is being typed as `string` rather than `Color`. Here are the errors: ``` This comparison appears to be unintentional because the types '"red"' and '"black"' have no overlap. This comparison appears to be unintentional because the types '"red"' and '"white"' have no overlap. ``` ### Reproduction https://github.com/cdcarson/svelte-lang-false-TS2367 ### Logs ```shell ``` ### System Info ```shell System: OS: macOS 26.3.1 CPU: (11) arm64 Apple M3 Pro Memory: 237.05 MB / 18.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 24.11.1 - /Users/cdcarson/.nvm/versions/node/v24.11.1/bin/node npm: 11.6.2 - /Users/cdcarson/.nvm/versions/node/v24.11.1/bin/npm pnpm: 10.33.0 - /Users/cdcarson/Library/pnpm/pnpm bun: 1.3.2 - /Users/cdcarson/.bun/bin/bun Watchman: 2026.03.30.00 - /opt/homebrew/bin/watchman Browsers: Chrome: 148.0.7778.168 Firefox: 144.0 Safari: 26.3.1 npmPackages: svelte: ^5.55.2 => 5.55.7 ``` ### Severity annoyance
关闭于 2026-05-16 0 条评论