-
-
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
Prop value can be different than guaranted by TypeScript #14725
Comments
Most likely same as #14707. Find a detailed explanation about this in that closed issue. |
I see, thanks. Maybe it could be mentioned in the breaking changes then? 🤔 I didn't see any info about this there. |
If it is not a bug, as #14707 suggests, could at least language tools be more useful in this case, if at all possible? A mismatch between type and runtime value will always be tricky to debug and a source of confusion. |
To me it seems that because the props are rather getters, the typing should be more pessimistic at the parent level. The only safe solution I can think of is that TS would not narrow the type of the prop in the getter positions: <script lang="ts">
const thing: string | undefined = $state('foo');
</script>
{#if thing !== undefined}
<Inner my_prop={thing} /> <!-- thing should be still string | undefined -->
{/if} Note that TypeScript does an equivalent type-checking here: |
I think it would be nice to get a warning in the |
We can't solve this at the type level, it would bring way too many false positives. It is inconvenient/surprising though, one could argue even a leak of the underlying signals implementation. Best would be to find a way to fix this. |
Describe the bug
This issue happens in Svelte 5 (both legacy and runes mode), but does not happen in Svelte 4.
Expected logs after clicking the
Reset value
button in the minimal example REPL:Actual logs (Svelte 5):
Note the TypeScript type shows the prop cannot be undefined.
Even though this would be a feature, it is quite inconvenient that the TypeScript type lies here. I was confused about this during a migration from Svelte 4 after I got a
TypeError
in runtime JS after accessing an attribute of a prop object that was actually undefined. I tried also the legacy mode, runes/stores and it is everytime the same behaviour in Svelte 5.Note that in Svelte 4 the output is correct - i.e. the prop is defined even in the onDestroy callback.
Reproduction
https://svelte.dev/playground/dc576c8a0dd94ff095ddb9a6976cfeaa?version=5.14.0
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: