Skip to content

Separate prop declarations in runes mode #14810

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
angryziber opened this issue Dec 23, 2024 · 1 comment
Closed

Separate prop declarations in runes mode #14810

angryziber opened this issue Dec 23, 2024 · 1 comment

Comments

@angryziber
Copy link

angryziber commented Dec 23, 2024

Describe the problem

The biggest downside of Svelte 5 compared to 4 is inability to declare props independently, each with it's type (yes, TypeScript) and default value. If you have many props in a generic component, most of them with default values, this gets pretty messy with $props().

Without runes you can write (clean):

<script lang="ts">
  export let name: string
  export let age: number = 10 // or you can even omit a type here and TS will still derive it
  // easy to add more props independently of other ones
<script>

With runes, it is no longer possible to declare both prop type and default value at the same time (messy, like in React):

<script lang="ts>
  let {
    name, 
    age = 10
  }: {
   name: string,
   age: number
  } = $props()
</script>

And the worst thing is that you can omit the type declaration if all of the props have default values (and TS will figure the types), but as long as you add a single prop without a default value, you are forced to type all the props.

Describe the proposed solution

As a solution it would be good to have a way to declare types one by one in rune mode, like

<script lang="ts">
  let name = $prop<string>()
  let age = $prop(3)
  let bindMe = $bindable<number>()
</script>

This will be consistent with $state() and also will allow TS to optionally declare or derive type of each prop independently from others. This will make complex prop declarations much cleaner if type-safety is desired (a very good idea for any bigger/important project).

Btw, $bindable() already exists and allows specifying of type of property at the declaration place, so the same functionaility is desired for non-bindable props.

Importance

I refuse to use runes without it

@angryziber angryziber changed the title Support for separate declaration of each prop in runes mode Separate prop declarations in runes mode Dec 23, 2024
@brunnerh
Copy link
Member

This proposal has been rejected previously, see lengthy discussion in:

@Conduitry Conduitry closed this as not planned Won't fix, can't repro, duplicate, stale Dec 23, 2024
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

3 participants