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: various snippet fixes #2449

Merged
merged 9 commits into from
Jul 30, 2024
Merged

Conversation

jasonlyu123
Copy link
Member

#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> => {

}

圖片

Another solution I considered using is an IIFE function so the inferred type is still Snippet<[...]>. But seems like typescript almost always expanded the type in hover. And

const foo = (() => {
    const $$_func = (a: string, b = 1) => { }

    return __sveltets_2_any() as Snippet<Parameters<typeof $$_func>>;
})()

圖片

I went for the ReturnType because the hover is shorter and the code generation is slightly simpler but OK with both so I could also change it to IIFE.

Breaking change

Since it is now possible to type snippets with JSDoc I removed explicit any in the generated code while changing the generation. This is a breaking change for js components with type-check but we can also add the any back for parameter without a default.

Scoping issues

The var is changed to const because this result in a runtime error but Typescript give a green light if it's var.

<div>
    {#snippet hi2()} {/snippet}
</div>

{@render hi2()}

The generated const is now hoist to before the first non-snippet sibling for #2417. And for implicit props, it's still passed directly to the component so the type can be inferred but another variable is generated for #2425

const {foo} = $$_tnenopmoC0.$$prop_def

A limitation is that we'll have to map the definition/reference/rename back but we can probably do this in a separate issue. Not even sure if many people actually rewrite something like this.

@jasonlyu123 jasonlyu123 marked this pull request as ready for review July 30, 2024 02:55
Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@dummdidumm dummdidumm merged commit 0f7bd88 into sveltejs:master Jul 30, 2024
3 checks passed
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