Skip to content

Conversation

@ryoppippi
Copy link

I would like to write the export statement for snapshot together with other export statements (e.g. export let data).
Therefore, I suggest that the capture/restore function be defined as a normal function, not as an arrow function.

@ciscoheat
Copy link
Owner

I'm not sure I understand what's the difference? The arrow function implicitly returns an object, and both functions have the same signature as the built-in version.

@ryoppippi
Copy link
Author

ryoppippi commented May 1, 2023

I know the type signature is the same, but here is the thing

When we use the current superform's capture and restore, and we write the export let snapshot above like,

<script lang="ts">
export let data;
export const snapshot: Snapshot = { capture, restore }

const { form, errors, enhance, delayed, message, capture, restore } = superForm(data.form, {
...
})
</script>

and execute svelte check, you can see the errors like this

/private/tmp/sveltekit-superforms/src/routes/snapshot/+page.svelte:10:5
Error: Block-scoped variable 'capture' used before its declaration. (ts)
  export const snapshot: Snapshot = { capture, restore }


/private/tmp/sveltekit-superforms/src/routes/snapshot/+page.svelte:10:5
Error: Variable 'capture' is used before being assigned. (ts)
  export const snapshot: Snapshot = { capture, restore }


/private/tmp/sveltekit-superforms/src/routes/snapshot/+page.svelte:10:5
Error: Block-scoped variable 'restore' used before its declaration. (ts)
  export const snapshot: Snapshot = { capture, restore }


/private/tmp/sveltekit-superforms/src/routes/snapshot/+page.svelte:10:5
Error: Variable 'restore' is used before being assigned. (ts)
  export const snapshot: Snapshot = { capture, restore }

However, when we implement these functions by non-arrorw-function, there is no error.

This is called hoisting
https://developer.mozilla.org/en-US/docs/Glossary/Hoisting

And When we use this hoisting function, we can make the code look natural.

@ciscoheat
Copy link
Owner

Right, hoisting. That should be no problem then, it'll be included in the next patch update.

@ciscoheat ciscoheat merged commit 8301819 into ciscoheat:main May 2, 2023
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

Successfully merging this pull request may close these issues.

2 participants