-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
$state(undefined)
is typed as never on svelte-check
#14435
Comments
I don't know where in the tool chain the problem is, so sorry if this is not the proper place to report it. Thanks for your hard work on Svelte! |
I believe the correct way to declare types for let p = $state<Person | undefined>(undefined); This will fix it Unsure why your way errors out though - The Typescript magic works in mysterious way |
From a TypeScript perspective this works as expected, because TypeScript does not see that |
Essentially duplicate of:
(Appeal n to move that kit issue.) |
Thanks for responses! Based on the received advise, I have confirmed that the following code fixes the error. let p1 = $state<Person>()
let p2 = $state<Person | undefined>()
let p3 = $state<Person | undefined>(undefined)
let p4: Person | undefined = $state()
let p5: Person | undefined = $state<Person | undefined>(undefined) |
Please let me know if it is better to close this issue and discuss it further in another issue. I understand now that if the type is not specified in generics, an error occurs due to a mismatch between the return type ( Person | undefined ) and the argument type ( undefined ) in the type inference, As an example, changing the type of $state as follows could also fix the error. declare function $state<T, U extends T = T>(initial: U): T; ( I am not used to typescript, so I can't judge if there is a problem with the code 💀 ) I think that maintainability, robustness, or other important features should take precedence over this, but I think it is simpler to write a state without explicit generics. type definitionsvelte/packages/svelte/types/index.d.ts Lines 2337 to 2338 in 19d80ad
|
Describe the bug
On a project generated by
[email protected]
, running a type check to the following code will get a result thatp
is typed asnever
.This is considered a bug because it does not occur when the following changes are made.
Reproduction
https://github.com/ykrods/svelte-reproduction-state-type
Logs
System Info
System: OS: Linux 6.10 Manjaro Linux CPU: (16) x64 AMD Ryzen 7 5700U with Radeon Graphics Memory: 8.43 GB / 14.98 GB Container: Yes Shell: 5.9 - /bin/zsh Binaries: Node: 22.8.0 - /usr/bin/node npm: 10.8.3 - /usr/bin/npm Browsers: Chromium: 131.0.6778.85 npmPackages: svelte: ^5.1.3 => 5.2.8
Severity
annoyance
The text was updated successfully, but these errors were encountered: