-
-
Notifications
You must be signed in to change notification settings - Fork 43
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
Svelte 5: When extracting props from the $props
rune typescript eslint errors with unsafe assignment, even though the type is defined following the documentation
#804
Comments
A workaround right now is to use a type assertion. <script lang="ts">
import type { Snippet } from 'svelte';
interface LayoutProps {
children: Snippet;
}
const { children } = $props() as LayoutProps;
</script>
{@render children()} Once this is fixed and the types generated by Svelte are used then this workaround should be removed. <script lang="ts">
const { children } = $props();
</script>
{@render children()} |
If I do the workaround, I get Typescript errors. The types Svelte generates (visible if you run props: Record<string, never>; That causes errors like this (seen through
Props have the right types if I don't use runes or don't assert types. |
Thank you for posting this issue! Hmm. It seems like a bug but the rule seems to work fine since it's actually |
I know this is more of a quetion for the Svelte team, but couldn't |
I forgot to comment here, but this issue may be resolved by updating this plugin and svelte-eslint-parser. Anyone with the same problem could you please try it?
svelte-eslint-parser now uses types like |
The reason why we don' use generics is sveltejs/svelte#10694 |
I think it's good to force users to write it like this: let { message }: { message: string } = $props(); However, because type inference works, we can still write that even if we define the type with For the ESLint plugin, the rule works well if type inference works on the return type of |
I'm not sure where it was corrected but I think now we don't get this error. |
Before You File a Bug Report Please Confirm You Have Done The Following...
What version of ESLint are you using?
9.5.0
What version of
eslint-plugin-svelte
are you using?2.40.0
What did you do?
Configuration
I created a new SvelteKit project and configured it with Svelte 5 to use runes. I used Bun as my package manager and runtime. After that I tried to create a layout file and use runes mode, but when setting up the
$props
so the layout could accept children I wasn't able to type it the same way that the Svelte's documentation uses it. I can work around it by addingas LayoutProps
after$props()
, but this feels wrong considering the documentation didn't explicitly say to typecast. I have also tested with non-route components and it also happens.What did you expect to happen?
I expected that when I typed the
$props
rune exactly as informed by the Svelte 5 preview documentation I would get no errors of unsafeany
assignments.What actually happened?
It detected getting the props for the component as an unsafe assignment.
Link to GitHub Repo with Minimal Reproducible Example
https://github.com/victoragcosta/eslint-plugin-svelte-props-unsafe-assignment-repro
Additional comments
Edit: I have tested it now and this also happens if I use
npm
andnode
instead ofbun
.The text was updated successfully, but these errors were encountered: