Skip to content

Svelte 5: $props() isn't generic anymore #10812

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

Closed
Aloso opened this issue Mar 15, 2024 · 10 comments
Closed

Svelte 5: $props() isn't generic anymore #10812

Aloso opened this issue Mar 15, 2024 · 10 comments

Comments

@Aloso
Copy link

Aloso commented Mar 15, 2024

Describe the bug

After updating to 5.0.0-next.79, I got type errors in all my components:

<script lang="ts">
  let { foo, bar, baz } = $props<SomeType>()
  //                             ^^^^^^^^^
  // Expected 0 type arguments, but got 1. ts(2558)
</script>

Instead, I need to write this:

  let { foo, bar, baz }: SomeType = $props()

Note that other runes, such as $state(), still support generics.

Reproduction

See above (the error appears in VS Code, but not in the web editor)

Logs

No response

System Info

System:
    OS: Linux 6.6 Manjaro Linux
    CPU: (12) x64 AMD Ryzen 5 5500U with Radeon Graphics
    Memory: 1.77 GB / 7.11 GB
    Container: Yes
    Shell: 5.2.26 - /bin/bash
  Binaries:
    Node: 21.6.1 - /usr/bin/node
    npm: 10.4.0 - /usr/bin/npm
    pnpm: 8.15.4 - /usr/bin/pnpm
  npmPackages:
    svelte: 5.0.0-next.79 => 5.0.0-next.79 
    svelte-check: ^3.6.7 => 3.6.7 
    tslib: ^2.6.2 => 2.6.2 
    typescript: ^5.4.2 => 5.4.2 
    vite: ^5.1.6 => 5.1.6

Severity

annoyance

@trueadm
Copy link
Contributor

trueadm commented Mar 15, 2024

This is intentional, see #10694

@trueadm trueadm closed this as completed Mar 15, 2024
@Aloso
Copy link
Author

Aloso commented Mar 15, 2024

@trueadm thanks.

This issue applies to $state() as well, if I'm not mistaken.

@trueadm
Copy link
Contributor

trueadm commented Mar 15, 2024

@Aloso $state has never been a generic AFAIK. Just type it like you do any other declaration.

let counter: number = $state(0);

@brunnerh
Copy link
Member

Still looks generic:

declare function $state<T>(initial: T): T;
declare function $state<T>(): T | undefined;

@trueadm
Copy link
Contributor

trueadm commented Mar 15, 2024

Sorry, I meant that it's never needed to be based a discrete generic. The inference system should play ball with the type annotation on the declaration – which is important as the initial value needs to align, thus generic is needed.

@Aloso
Copy link
Author

Aloso commented Mar 15, 2024

The generic is often required. For example:

let s = $state<'idle' | 'loading' | 'done'>('idle')

@trueadm
Copy link
Contributor

trueadm commented Mar 15, 2024

Why can't you do?

let s: 'idle' | 'loading' | 'done' = $state('idle')

@Aloso
Copy link
Author

Aloso commented Mar 16, 2024

@trueadm of course this works, but the function still needs to be generic. If you made it

function $state(initial: any): any

Then it wouldn't be type-safe.

@trueadm
Copy link
Contributor

trueadm commented Mar 16, 2024

@Aloso We are not going to make it any, in fact we plan on keeping it a generic as my reply above states. Why do you think we plan on changing it?

gursheyss added a commit to SCE-Development/spartan-compass-v0 that referenced this issue Mar 20, 2024
@sysmat
Copy link

sysmat commented Oct 25, 2024

  • uff shit, you want to ts infer type
  • you need to create wrappers types for components, boilerplate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants