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

feat: add ComponentExports utility type #13441

Merged
merged 6 commits into from
Oct 31, 2024

Conversation

FoHoOV
Copy link
Contributor

@FoHoOV FoHoOV commented Sep 29, 2024

With the type change for bind:this on components (related issues are this language-tools issue and 13430).
Recommended way is to do this (per this comment):

<script lang="ts">
    import CloneWorkflowDialog from './dialogs/CloneWorkflowDialog.svelte';

-   let cloneWorkflowDialog : CloneWorkflowDialog;
+   let cloneWorkflowDialog : ReturnType<typeof CloneWorkflowDialog>;
</script>

Since we currently have the Component type exported by svelte that has the Exports we can rely on that and do this instead:

<script lang="ts">
    import CloneWorkflowDialog from './dialogs/CloneWorkflowDialog.svelte';
    // in a utility type, preferably provided by Svelte package itself. 
+   type ComponentExports<TComponent extends Component<any, any>> =
+	TComponent extends Component<any, infer TExports> ? TExports : never;
 
-  let cloneWorkflowDialog : ReturnType<typeof CloneWorkflowDialog>;
+  let cloneWorkflowDialog : ComponentExports<typeof CloneWorkflowDialog>;
</script>

Whats the problem with using ReturnType?

This is the safer/forward-compatibale way that the svelte can decide on the type.

Svelte 5 rewrite

Please note that the Svelte codebase is currently being rewritten for Svelte 5. Changes should target Svelte 5, which lives on the default branch (main).

If your PR concerns Svelte 4 (including updates to svelte.dev.docs), please ensure the base branch is svelte-4 and not main.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Sep 29, 2024

🦋 Changeset detected

Latest commit: 46a0488

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

This PR includes changesets to release 1 package
Name Type
svelte 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

@webJose
Copy link
Contributor

webJose commented Sep 29, 2024

Note that some previous change (which may be the one being referred to here) broke @testing-library/svelte's TypeScript support. I mention this here because perhaps it could be verified.

@bdmackie
Copy link

bdmackie commented Sep 30, 2024

Very cool, so much nicer to code with. I get this warning in VS code, does this have to be suppressed globally or is there a better way to avoid it?

dropdownComp1 is updated, but is not declared with $state(...). Changing its value will not correctly trigger updatessvelte(non_reactive_update)

Update: I believe it's required to ignore the above warning. See my comment here for my use case and the related discussion: #13104 (comment)

@FoHoOV
Copy link
Contributor Author

FoHoOV commented Sep 30, 2024

Very cool, so much nicer to code with. I get this warning in VS code, does this have to be suppressed globally or is there a better way to avoid it?

dropdownComp1 is updated, but is not declared with $state(...). Changing its value will not correctly trigger updatessvelte(non_reactive_update)

Update: I believe it's required to ignore the above warning. See my comment here for my use case and the related discussion: #13104 (comment)

If I understand correctly this is not related to this PR, this is just a utility type, to type the bind:this (since let component: Component is not the correct type anymore), this will be forward compatible if the type is gonna change in the future as well.

@bdmackie
Copy link

Very cool, so much nicer to code with. I get this warning in VS code, does this have to be suppressed globally or is there a better way to avoid it?
dropdownComp1 is updated, but is not declared with $state(...). Changing its value will not correctly trigger updatessvelte(non_reactive_update)
Update: I believe it's required to ignore the above warning. See my comment here for my use case and the related discussion: #13104 (comment)

If I understand correctly this is not related to this PR, this is just a utility type, to type the bind:this (since let component: Component is not the correct type anymore), this will be forward compatible if the type is gonna change in the future as well.

I tried to say it was just a use case connection (adoption friction for bind:this) for context if anyone was prioritising/triaging, so not technical/code related to this PR specifically. Apologies if it was unhelpful.

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.

thank you!

Copy link

pkg-pr-new bot commented Oct 31, 2024

pnpm add https://pkg.pr.new/svelte@13441

commit: 46a0488

@dummdidumm dummdidumm merged commit d93ad3b into sveltejs:main Oct 31, 2024
9 checks passed
@github-actions github-actions bot mentioned this pull request Oct 31, 2024
Rich-Harris added a commit that referenced this pull request Oct 31, 2024
@FoHoOV FoHoOV deleted the add-component-exports-type branch October 31, 2024 14:56
@Rich-Harris
Copy link
Member

Thank you — we decided against releasing this in favour of making let thing: Thing work everywhere

dummdidumm pushed a commit that referenced this pull request Oct 31, 2024
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.

5 participants