svelte icon indicating copy to clipboard operation
svelte copied to clipboard

feat: add `ComponentExports` utility type

Open FoHoOV opened this issue 1 year ago • 5 comments

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

  • [x] 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
  • [x] Prefix your PR title with feat:, fix:, chore:, or docs:.
  • [x] 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

FoHoOV avatar Sep 29 '24 21:09 FoHoOV

🦋 Changeset detected

Latest commit: 46a0488c7609d1ece0a532b8acdb03dc3e883166

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

changeset-bot[bot] avatar Sep 29 '24 21:09 changeset-bot[bot]

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.

webJose avatar Sep 29 '24 22:09 webJose

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: https://github.com/sveltejs/svelte/pull/13104#issuecomment-2331368432

bdmackie avatar Sep 30 '24 00:09 bdmackie

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.

FoHoOV avatar Sep 30 '24 10:09 FoHoOV

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.

bdmackie avatar Sep 30 '24 11:09 bdmackie

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

commit: 46a0488

pkg-pr-new[bot] avatar Oct 31 '24 11:10 pkg-pr-new[bot]

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

Rich-Harris avatar Oct 31 '24 14:10 Rich-Harris