Skip to content

Commit f327425

Browse files
committed
breaking: remove Component type, keep using SvelteComponent instead
I came to the conclusion that when we're making up arbitrary types, we might as well keep the old class. That way: - one less thing to worry about (language tools and other tooling basically can continue to spit out SvelteComponent ) - we can more clearly mark $set , the constructor etc as being deprecated and no longer functioning unless you use that legacy compatibility mode - much more ergonomic to type for the user: - const someInstance: SvelteComponent<..> instead of const someInstance: ReturnType<typeof Component<..>> - If you're using generics, you can do export class MyComponent<T> extends SvelteComponent<{ prop: T }> {} instead of having to type out the whole function in a way that I'm not even sure how to do with generics
1 parent 6d7caf3 commit f327425

File tree

8 files changed

+162
-185
lines changed

8 files changed

+162
-185
lines changed

.changeset/small-papayas-laugh.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
breaking: remove Component type, keep using SvelteComponent instead

packages/svelte/src/internal/client/render.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3020,7 +3020,7 @@ export function unwrap(value) {
30203020
* @template {Record<string, any>} Props
30213021
* @template {Record<string, any> | undefined} Exports
30223022
* @template {Record<string, any>} Events
3023-
* @param {import('../../main/public.js').Component<Props, Exports, Events>} component
3023+
* @param {import('../../main/public.js').SvelteComponent<Props, Events>} component
30243024
* @param {{
30253025
* target: Node;
30263026
* props?: Props;
@@ -3139,7 +3139,7 @@ export function createRoot(component, options) {
31393139
* @template {Record<string, any>} Props
31403140
* @template {Record<string, any> | undefined} Exports
31413141
* @template {Record<string, any>} Events
3142-
* @param {import('../../main/public.js').Component<Props, Exports, Events>} component
3142+
* @param {import('../../main/public.js').SvelteComponent<Props, Events>} component
31433143
* @param {{
31443144
* target: Node;
31453145
* props?: Props;

packages/svelte/src/legacy/legacy-client.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import * as $ from '../internal/index.js';
1111
* @template {Record<string, any>} Events
1212
* @template {Record<string, any>} Slots
1313
*
14-
* @param {import('./public.js').ComponentConstructorOptions<Props> & {
15-
* component: import('../main/public.js').Component<Props, Exports, Events, Slots>;
14+
* @param {import('../main/public.js').ComponentConstructorOptions<Props> & {
15+
* component: import('../main/public.js').SvelteComponent<Props, Events, Slots>;
1616
* immutable?: boolean;
1717
* recover?: false;
1818
* }} options
19-
* @returns {import('./public.js').SvelteComponent<Props & Exports, Events, Slots>}
19+
* @returns {import('../main/public.js').SvelteComponent<Props, Events, Slots> & Exports}
2020
*/
2121
export function createClassComponent(options) {
2222
// @ts-expect-error $$prop_def etc are not actually defined
@@ -33,8 +33,8 @@ export function createClassComponent(options) {
3333
* @template {Record<string, any>} Events
3434
* @template {Record<string, any>} Slots
3535
*
36-
* @param {import('../main/public.js').Component<Props, Exports, Events, Slots>} component
37-
* @returns {typeof import('./public.js').SvelteComponent<Props & Exports, Events, Slots>}
36+
* @param {import('../main/public.js').SvelteComponent<Props, Events, Slots>} component
37+
* @returns {typeof import('../main/public.js').SvelteComponent<Props, Events, Slots> & Exports}
3838
*/
3939
export function asClassComponent(component) {
4040
// @ts-expect-error $$prop_def etc are not actually defined
@@ -57,7 +57,7 @@ class Svelte4Component {
5757
#instance;
5858

5959
/**
60-
* @param {import('./public.js').ComponentConstructorOptions & {
60+
* @param {import('../main/public.js').ComponentConstructorOptions & {
6161
* component: any;
6262
* immutable?: boolean;
6363
* recover?: false;

packages/svelte/src/legacy/legacy-server.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { render } from '../internal/server/index.js';
66
export { createClassComponent };
77

88
/**
9-
* Takes the component function and returns a Svelte 4 compatible component constructor.
9+
* Takes a Svelte 5 component and returns a Svelte 4 compatible component constructor.
1010
*
1111
* @deprecated Use this only as a temporary solution to migrate your imperative component code to Svelte 5.
1212
*
@@ -15,8 +15,8 @@ export { createClassComponent };
1515
* @template {Record<string, any>} Events
1616
* @template {Record<string, any>} Slots
1717
*
18-
* @param {import('../main/public.js').Component<Props, Exports, Events, Slots>} component
19-
* @returns {typeof import('./public.js').SvelteComponent<Props & Exports, Events, Slots>}
18+
* @param {import('../main/public.js').SvelteComponent<Props, Events, Slots>} component
19+
* @returns {typeof import('../main/public.js').SvelteComponent<Props, Events, Slots> & Exports}
2020
*/
2121
export function asClassComponent(component) {
2222
const component_constructor = as_class_component(component);
@@ -30,8 +30,9 @@ export function asClassComponent(component) {
3030
html: result.html
3131
};
3232
};
33-
// @ts-expect-error this is present for SSR
33+
// this is present for SSR
3434
component_constructor.render = _render;
3535

36+
// @ts-ignore
3637
return component_constructor;
3738
}
Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1 @@
1-
/**
2-
* @deprecated Use `Component` instead. See TODO for more information.
3-
*/
4-
export interface ComponentConstructorOptions<
5-
Props extends Record<string, any> = Record<string, any>
6-
> {
7-
target: Element | Document | ShadowRoot;
8-
anchor?: Element;
9-
props?: Props;
10-
context?: Map<any, any>;
11-
hydrate?: boolean;
12-
intro?: boolean;
13-
$$inline?: boolean;
14-
}
15-
16-
/**
17-
* @deprecated use `Component` instead. See TODO for more information.
18-
*
19-
* Base class for Svelte components in Svelte 4. Svelte 5+ components implement
20-
* the `Component` interface instead. This class is only provided for backwards
21-
* compatibility with Svelte 4 typings and doesn't have any runtime equivalent.
22-
*
23-
* Can be used to create strongly typed Svelte components.
24-
*
25-
* #### Example:
26-
*
27-
* You have component library on npm called `component-library`, from which
28-
* you export a component called `MyComponent`. For Svelte+TypeScript users,
29-
* you want to provide typings. Therefore you create a `index.d.ts`:
30-
* ```ts
31-
* import { SvelteComponent } from "svelte";
32-
* export class MyComponent extends SvelteComponent<{foo: string}> {}
33-
* ```
34-
* Typing this makes it possible for IDEs like VS Code with the Svelte extension
35-
* to provide intellisense and to use the component like this in a Svelte file
36-
* with TypeScript:
37-
* ```svelte
38-
* <script lang="ts">
39-
* import { MyComponent } from "component-library";
40-
* </script>
41-
* <MyComponent foo={'bar'} />
42-
* ```
43-
*/
44-
export class SvelteComponent<
45-
Props extends Record<string, any> = any,
46-
Events extends Record<string, any> = any,
47-
Slots extends Record<string, any> = any
48-
> {
49-
[prop: string]: any;
50-
51-
constructor(options: ComponentConstructorOptions<Props>);
52-
/**
53-
* For type checking capabilities only.
54-
* Does not exist at runtime.
55-
* ### DO NOT USE!
56-
*
57-
* */
58-
$$prop_def: Props;
59-
/**
60-
* For type checking capabilities only.
61-
* Does not exist at runtime.
62-
* ### DO NOT USE!
63-
*
64-
* */
65-
$$events_def: Events;
66-
/**
67-
* For type checking capabilities only.
68-
* Does not exist at runtime.
69-
* ### DO NOT USE!
70-
*
71-
* */
72-
$$slot_def: Slots;
73-
74-
$destroy(): void;
75-
76-
$on<K extends Extract<keyof Events, string>>(
77-
type: K,
78-
callback: (e: Events[K]) => void
79-
): () => void;
80-
81-
$set(props: Partial<Props>): void;
82-
}
83-
84-
/**
85-
* @deprecated Use `Component` instead. See TODO for more information.
86-
*/
87-
export class SvelteComponentTyped<
88-
Props extends Record<string, any> = any,
89-
Events extends Record<string, any> = any,
90-
Slots extends Record<string, any> = any
91-
> extends SvelteComponent<Props, Events, Slots> {}
92-
931
export * from './legacy-client.js';

packages/svelte/src/main/ambient.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare module '*.svelte' {
2-
export { Component as default } from 'svelte';
2+
export { SvelteComponent as default } from 'svelte';
33
}
44

55
/**

0 commit comments

Comments
 (0)