Skip to content
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

Should it be possible to type snippet args using JSDoc? #2444

Closed
brunnerh opened this issue Apr 25, 2024 · 3 comments
Closed

Should it be possible to type snippet args using JSDoc? #2444

brunnerh opened this issue Apr 25, 2024 · 3 comments
Labels
feature request New feature or request Fixed Fixed in master branch. Pending production release.

Comments

@brunnerh
Copy link
Member

Describe the bug

Snippets that are passed to components can be typed as parts of $props but how about snippets defined and used locally?

Workaround: Just use TS.

Reproduction

{#snippet test(/** @type {number} */ arg)}
	{arg}
{/snippet}

Expected identifier or destructure pattern

Logs

No response

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
    Memory: 10.15 GB / 31.92 GB
  Binaries:
    Node: 18.19.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 9.2.0 - ~\AppData\Roaming\npm\npm.CMD
    pnpm: 8.6.9 - ~\AppData\Local\pnpm\pnpm.EXE
  Browsers:
    Chrome: 123.0.6312.124
    Edge: Spartan (), Chromium (123.0.2420.97), ChromiumDev ()
    Internet Explorer: 11.0.19041.3636

Severity

annoyance

@xeho91
Copy link

xeho91 commented May 16, 2024

Alternative to consider - HTML comment:

<!-- 
Test function that takes a single string argument.
@param {string} arg - The string argument to be processed.
-->
{#snippet test(arg)}

{/snippet}

Credits goes to Hugos from Svelte Discord!

@dummdidumm
Copy link
Member

Comments inside the template are now allowed, but we likely need some more adjustments in language-tools to make this work, therefore moving this issue there

@dummdidumm dummdidumm added the feature request New feature or request label Jul 24, 2024
@dummdidumm dummdidumm transferred this issue from sveltejs/svelte Jul 24, 2024
@dummdidumm dummdidumm added the Fixed Fixed in master branch. Pending production release. label Jul 30, 2024
dummdidumm pushed a commit that referenced this issue Jul 30, 2024
#2444 #2417 #2425 #2385 (comment)

The snippet is now transformed into a function with the return type so that The default parameter type can be inferred:

`const foo = (a: string, b = 1): ReturnType<import('svelte').Snippet> => {}`
@brunnerh
Copy link
Member Author

Note on usage:

  • /** @type {...} */ can now be used on snippet arguments.
  • To flag type errors, // @ts-check appears to be necessary at the start of the <script>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request Fixed Fixed in master branch. Pending production release.
Projects
None yet
Development

No branches or pull requests

3 participants