|
|
|
|
|
by doctor_eval
861 days ago
|
|
But doesn't export let myProp = 10
work in any JS module? I thought it was quite standard. Maybe I'm mistaken, JS/TS is not my first language, but I think it's pretty obvious what it's doing.In any case, let { myProp = 10 } = $props
does not even remotely work like it would in JS. It's literally a compiler directive.That is, $props is not an object. The string "$props" is a compile-time directive that declares myProp to be a prop. It's not destructuring $props, it's declaring myProp. "$props" is a magic incantation defined within the Svelte compiler. I mean, this is my point really. It's using JS syntax but it's no longer obvious what it's doing. To your point, given that Svelte compiles the code anyway, I don't understand why they couldn't have kept the existing syntax ("vibe") and just changed the semantics. Why come up with some brand new, syntactically-compatible- but semantically-incompatible-with-JS, syntax? |
|
In the end it is also just a compiler directive for svelte, it doesn't end up in the code the svelte compiler outputs.
Look at this example:
https://svelte.dev/repl/c450c88a415a435485729f4ba88069f1?ver...
If you check the JavaScript output tab on right, this prop declaration in Child.svelte
> export let count = 0;
Has become
> let { count = 0 } = $$props;
Which looks a lot closer to this new rune syntax, so
> does not even remotely work like it would in JS.
Is not true at all, it works a lot like this in JS, or at least a lot more than the "export let" syntax.
Edit:
I want to add to your point here:
> That is, $props is not an object. The string "$props" is a compile-time directive that declares myProp to be a prop. It's not destructuring $props, it's declaring myProp. "$props" is a magic incantation defined within the Svelte compiler.
I get you on this! I wasn't aware it worked like this and replaces the $$Props interface [1] which I am using to declare component props at the moment. I understand your point and I agree that parsing required props and such out of this destructuring assignment is a bit magical, but to be honest in my view not significantly more magical than the rest of Svelte.
[1] https://svelte-5-preview.vercel.app/docs/runes#$props-what-t...