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

breaking: deprecate SvelteComponentTyped, add generics to SvelteComponent #8512

Merged
merged 3 commits into from
Apr 24, 2023

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Apr 18, 2023

Breaking change / how to migrate

SvelteComponent now has the same capabilities as SvelteComponenTyped, which is therefore now deprecated. Use SvelteComponent where you've used SvelteComponentTyped previously:

- import { SvelteComponentTyped } from 'svelte';
+ import { SvelteComponent } from 'svelte';

- export class Foo extends SvelteComponentTyped<{ aProp: string }> {}
+ export class Foo extends SvelteComponent<{ aProp: string }> {}

In case you've used SvelteComponent previously to type "I expect a Svelte component constructor here" by using typeof SvelteComponent, you'll now likely get a somewhat opaque TypeScript error.

Code:

import type { SvelteComponent } from 'svelte';

const a: typeof SvelteComponent = SomeSvelteComponentWithAProp;

Error:

Type 'typeof SomeSvelteComponentWithAProp' is not assignable to type 'typeof SvelteComponent'.
  Types of construct signatures are incompatible.
    Type 'new (options: ComponentConstructorOptions<{ prop: string; }>) => SomeSvelteComponentWithAProp' is not assignable to type 'new <Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any>(options: ComponentConstructorOptions<Props>) => SvelteComponent<...>'.
      Types of parameters 'options' and 'options' are incompatible.
        Type 'ComponentConstructorOptions<Props>' is not assignable to type 'ComponentConstructorOptions<{ prop: string; }>'.
          Type 'Props' is not assignable to type '{ prop: string; }'.
            Property 'prop' is missing in type 'Record<string, any>' but required in type '{ prop: string; }'.

The solution is to either add <any> to typeof SvelteComponent (which is possible since TypeScript version 4.7) or do ComponentType<SvelteComponent> instead:

import type { SvelteComponent, ComponentType } from 'svelte';

// either
const a: typeof SvelteComponent<any> = SomeSvelteComponentWithAProp;
// or
const b: ComponentType<SvelteComponent> = SomeSvelteComponentWithAProp;

PR description

Deprecate SvelteComponentTyped, add generics to SvelteComponent - reverts the revert in #5738. Also add data- attribute to HTMLAttributes (for sveltejs/language-tools#1825) and use available TS interfaces.

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

  • Run the tests with npm test and lint the project with npm run lint

…nent

Also add data- attribute to HTMLAttributes and use available TS interfaces
@vercel
Copy link

vercel bot commented Apr 18, 2023

@dummdidumm is attempting to deploy a commit to the Svelte Team on Vercel.

A member of the Team first needs to authorize it.

@dummdidumm dummdidumm added this to the 4.x milestone Apr 18, 2023
* const component: typeof SvelteComponent = ASubclassOfSvelteComponent;
* ```
* will throw a type error, so we need to separate the more strictly typed class.
* @deprecated Use `SvelteComponent` instead. See PR for more information: https://github.com/sveltejs/svelte/pull/8512
Copy link
Member

@baseballyama baseballyama Apr 23, 2023

Choose a reason for hiding this comment

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

Can we remove SvelteComponentTyped completely if we will release this PR in Svelte4 because this is a major version up?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could but I think it's better to do that in Svelte 5 to give people time to gracefully update their code. Also if some use components built for Svelte 3 which you can otherwise use in Svelte 4 we shouldn't block that just because the class was removed. Right now svelte package uses SvelteCompentTyped in the generated d.ts files.

@dummdidumm dummdidumm merged commit 572f537 into sveltejs:version-4 Apr 24, 2023
@gtm-nayan gtm-nayan mentioned this pull request Jun 17, 2023
5 tasks
jiangfengming added a commit to svelte-pilot/svelte-pilot that referenced this pull request Jun 27, 2023
@dummdidumm dummdidumm deleted the svelte-component-typed branch September 8, 2023 14:15
Rinrin0413 added a commit to Rinrin0413/rinrin0413.github.io that referenced this pull request Sep 27, 2023
]

`SvelteComponentTyped` is deprecated in Svelte 4.
I gonna move to Svelte 4 in the future so.

See also: sveltejs/svelte#8512
fmaclen added a commit to fmaclen/svelte-currency-input that referenced this pull request Nov 10, 2023
- REF https://kit.svelte.dev/docs/packaging#anatomy-of-a-package-json-svelte
- Downgrade to Svelte 3.59.2 because 4.0.0 exports the package using a type `SvelteComponent` which causes the types to break when used in Svelte 3.x — REF sveltejs/svelte#8512

Fixes #68
fmaclen added a commit to fmaclen/svelte-currency-input that referenced this pull request Nov 10, 2023
- REF https://kit.svelte.dev/docs/packaging#anatomy-of-a-package-json-svelte
- Downgrade to Svelte 3.59.2 because 4.0.0 exports the package using a type `SvelteComponent` which causes the types to break when used in Svelte 3.x — REF sveltejs/svelte#8512

Fixes #68
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants