Skip to content

Commit 99000ef

Browse files
authored
typed SvelteComponent(Dev) interface (#5431)
1 parent 201b057 commit 99000ef

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
* Add a typed `SvelteComponent` interface ([#5431](https://github.com/sveltejs/svelte/pull/5431))
56
* Fix setting reactive dependencies which don't appear in the template to `undefined` ([#5538](https://github.com/sveltejs/svelte/issues/5538))
67
* Support preprocessor sourcemaps during compilation ([#5584](https://github.com/sveltejs/svelte/pull/5584))
78
* Fix ordering of elements when using `{#if}` inside `{#key}` ([#5680](https://github.com/sveltejs/svelte/issues/5680))

src/runtime/internal/Component.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,19 @@ if (typeof HTMLElement === 'function') {
212212
};
213213
}
214214

215-
export class SvelteComponent {
215+
export class SvelteComponent<
216+
Props extends Record<string, any> = any,
217+
Events extends Record<string, any> = any
218+
> {
216219
$$: T$$;
217-
$$set?: ($$props: any) => void;
220+
$$set?: ($$props: Partial<Props>) => void;
218221

219222
$destroy() {
220223
destroy_component(this, 1);
221224
this.$destroy = noop;
222225
}
223226

224-
$on(type, callback) {
227+
$on<K extends Extract<keyof Events, string>>(type: K, callback: (e: Events[K]) => void) {
225228
const callbacks = (this.$$.callbacks[type] || (this.$$.callbacks[type] = []));
226229
callbacks.push(callback);
227230

@@ -231,7 +234,7 @@ export class SvelteComponent {
231234
};
232235
}
233236

234-
$set($$props) {
237+
$set($$props: Partial<Props>) {
235238
if (this.$$set && !is_empty($$props)) {
236239
this.$$.skip_bound = true;
237240
this.$$set($$props);

src/runtime/internal/dev.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,52 @@ export function validate_slots(name, slot, keys) {
9797
}
9898
}
9999

100-
type Props = Record<string, any>;
101-
export interface SvelteComponentDev {
102-
$set(props?: Props): void;
103-
$on<T = any>(event: string, callback: (event: CustomEvent<T>) => void): () => void;
100+
export interface SvelteComponentDev<
101+
Props extends Record<string, any> = any,
102+
Events extends Record<string, any> = any,
103+
Slots extends Record<string, any> = any
104+
> {
105+
$set(props?: Partial<Props>): void;
106+
$on<K extends Extract<keyof Events, string>>(type: K, callback: (e: Events[K]) => void): () => void;
104107
$destroy(): void;
105108
[accessor: string]: any;
106109
}
107110

108-
export class SvelteComponentDev extends SvelteComponent {
111+
export class SvelteComponentDev<
112+
Props extends Record<string, any> = any,
113+
Events extends Record<string, any> = any,
114+
Slots extends Record<string, any> = any
115+
> extends SvelteComponent<Props, Events> {
116+
/**
117+
* @private
118+
* For type checking capabilities only.
119+
* Does not exist at runtime.
120+
* ### DO NOT USE!
121+
*/
122+
$$prop_def: Props;
123+
/**
124+
* @private
125+
* For type checking capabilities only.
126+
* Does not exist at runtime.
127+
* ### DO NOT USE!
128+
*/
129+
$$events_def: Events;
130+
/**
131+
* @private
132+
* For type checking capabilities only.
133+
* Does not exist at runtime.
134+
* ### DO NOT USE!
135+
*/
136+
$$slot_def: Slots;
137+
109138
constructor(options: {
110139
target: Element;
111140
anchor?: Element;
112141
props?: Props;
113142
hydrate?: boolean;
114143
intro?: boolean;
115144
$$inline?: boolean;
116-
}) {
145+
}) {
117146
if (!options || (!options.target && !options.$$inline)) {
118147
throw new Error("'target' is a required option");
119148
}

0 commit comments

Comments
 (0)