-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
🦋 Changeset detectedLatest commit: dd14843 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
/^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 = ` | ||
); |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, impressive fix!
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 fromrender().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 SvelteNow we use
svelte2tsx
's__sveltets_1_partial
return type, which through TS trickery return the proper types and information for all kinds of situationsFix withastro/language-tools#314
Testing
Tested manually against withastro/language-tools#344
Docs
N/A