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

fix: support default parameter in snippet block #2385

Closed

Conversation

jasonlyu123
Copy link
Member

@jasonlyu123 jasonlyu123 marked this pull request as draft June 3, 2024 04:23
@jasonlyu123
Copy link
Member Author

Still need to find a way to infer the type from the default value.

@dummdidumm
Copy link
Member

What do you mean by "infer the type from the default value"?
Also, this feels like something we could fix in the Svelte parser instead - feels wrong that start/end isn't set for it.

dummdidumm added a commit to sveltejs/svelte that referenced this pull request Jun 6, 2024
We should add them everywhere we can
Related sveltejs/language-tools#2385
@jasonlyu123
Copy link
Member Author

jasonlyu123 commented Jun 6, 2024

What do you mean by "infer the type from the default value"?

Because of the type annotation, the type for the parameter would be any instead of inferring from the default value like functions usually do.

This feels like something we could fix in the Svelte parser instead.

Sure. We can deal with this type of problem later. I can't really think of a way to let ts infer the type now.

Rich-Harris added a commit to sveltejs/svelte that referenced this pull request Jun 6, 2024
* fix: append start/end info to more tags

We should add them everywhere we can
Related sveltejs/language-tools#2385

* changeset

---------

Co-authored-by: Rich Harris <rich.harris@vercel.com>
@dummdidumm
Copy link
Member

dummdidumm commented Jun 7, 2024

I pushed a fix to Svelte to make the default parameter appear correctly. What's left is the default type inference problem.

We could do something like: check if the default parameter references a variable outside the snippet code

  • if yes, then do this generation": : Snippet<[typeof defaultValue]
  • if not, assign default value to a bogus variable and then do typeof: const __asd__ = 'default value literal'.... : Snippet<[typeof __asd__]>

@dummdidumm
Copy link
Member

Closing in favor of #2449

@dummdidumm dummdidumm closed this Jul 30, 2024
dummdidumm pushed a commit that referenced this pull request 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> => {}`
This pull request was closed.
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