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

Properly support and type optional props in Svelte components #3993

Merged
merged 3 commits into from
Jul 20, 2022

Conversation

Princesseuh
Copy link
Member

@Princesseuh Princesseuh commented Jul 20, 2022

Changes

This changes the shape of the generated TSX for Svelte components to be more in line with how Svelte's tooling actually work

Previously, what we did was skip over every svelte2tsx type and just make our own type from render().props, this works for getting a list of the props, but it doesn't work for optional props as those are still included in the list and we don't have the needed type info to know it's optional or not due to how optional props work in Svelte

Now we use svelte2tsx's __sveltets_1_partial return type, which through TS trickery return the proper types and information for all kinds of situations

Fix withastro/language-tools#314

Testing

Tested manually against withastro/language-tools#344

Docs

N/A

@changeset-bot
Copy link

changeset-bot bot commented Jul 20, 2022

🦋 Changeset detected

Latest commit: dd14843

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@astrojs/svelte Patch
@e2e/errors Patch
@e2e/svelte-component Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Princesseuh Princesseuh self-assigned this Jul 20, 2022
@github-actions github-actions bot added pkg: svelte Related to Svelte (scope) pkg: integration Related to any renderer integration (scope) labels Jul 20, 2022
@Princesseuh Princesseuh marked this pull request as ready for review July 20, 2022 20:58
@Princesseuh Princesseuh changed the title [WIP] Properly support and type optional props in Svelte components Properly support and type optional props in Svelte components Jul 20, 2022
/^export default[\S\s]*/gm,
`export default function ${className}__AstroComponent_(_props: typeof Props): any {}`
'export default class extends __sveltets_1_createSvelte2TsxComponent(',
`export default function ${className}__AstroComponent_(_props: typeof Component.props): any {}\nlet Component = `
);
Copy link
Member Author

@Princesseuh Princesseuh Jul 20, 2022

Choose a reason for hiding this comment

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

TLDR:

Svelte Component

<script lang="ts">
  export const hello = "Hello";
  export const wow = "Hello";
  export let hey: string;
</script>
{hey}

Old

let Props = render().props;
///<reference types="svelte" />
;function render() {

   const hello = "Hello";
   const wow = "Hello";
   let hey: string/*Ωignore_startΩ*/;hey = __sveltets_1_any(hey);/*Ωignore_endΩ*/;
;
async () => {

hey;

};
return { props: {hello: hello , wow: wow , hey: hey}, slots: {}, getters: {hello: hello, wow: wow}, events: {} }}

export default function MySvelteComponent__AstroComponent_(_props: typeof Props): any {}

New

/// <reference types="svelte2tsx/svelte-shims" />
///<reference types="svelte" />
;function render() {

   const hello = "Hello";
   const wow = "Hello";
   let hey: string/*Ωignore_startΩ*/;hey = __sveltets_1_any(hey);/*Ωignore_endΩ*/;
;
async () => {

hey;
};
return { props: {hello: hello , wow: wow , hey: hey}, slots: {}, getters: {hello: hello, wow: wow}, events: {} }}

export default function MySvelteComponent__AstroComponent_(_props: typeof Component.props): any {}
let Component = __sveltets_1_partial(['hello','wow'], __sveltets_1_with_any_event(render()))) {
    get hello() { return render().getters.hello }
    get wow() { return render().getters.wow }
}

(The export being on top of the let Component is on purpose, so the component documentation ends up on the exported function)

Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

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

Wow, impressive fix!

@Princesseuh Princesseuh merged commit 40a45e3 into main Jul 20, 2022
@Princesseuh Princesseuh deleted the svelte-optional-props branch July 20, 2022 21:32
@astrobot-houston astrobot-houston mentioned this pull request Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: integration Related to any renderer integration (scope) pkg: svelte Related to Svelte (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: Svelte optional props: code works but vscode complains
2 participants