typed SvelteComponent(Dev) interface (#5431)

pull/5723/head
Simon H 4 years ago committed by GitHub
parent 201b057ec5
commit 99000ef42e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -212,16 +212,19 @@ if (typeof HTMLElement === 'function') {
}; };
} }
export class SvelteComponent { export class SvelteComponent<
Props extends Record<string, any> = any,
Events extends Record<string, any> = any
> {
$$: T$$; $$: T$$;
$$set?: ($$props: any) => void; $$set?: ($$props: Partial<Props>) => void;
$destroy() { $destroy() {
destroy_component(this, 1); destroy_component(this, 1);
this.$destroy = noop; this.$destroy = noop;
} }
$on(type, callback) { $on<K extends Extract<keyof Events, string>>(type: K, callback: (e: Events[K]) => void) {
const callbacks = (this.$$.callbacks[type] || (this.$$.callbacks[type] = [])); const callbacks = (this.$$.callbacks[type] || (this.$$.callbacks[type] = []));
callbacks.push(callback); callbacks.push(callback);
@ -231,7 +234,7 @@ export class SvelteComponent {
}; };
} }
$set($$props) { $set($$props: Partial<Props>) {
if (this.$$set && !is_empty($$props)) { if (this.$$set && !is_empty($$props)) {
this.$$.skip_bound = true; this.$$.skip_bound = true;
this.$$set($$props); this.$$set($$props);

@ -97,15 +97,44 @@ export function validate_slots(name, slot, keys) {
} }
} }
type Props = Record<string, any>; export interface SvelteComponentDev<
export interface SvelteComponentDev { Props extends Record<string, any> = any,
$set(props?: Props): void; Events extends Record<string, any> = any,
$on<T = any>(event: string, callback: (event: CustomEvent<T>) => void): () => void; Slots extends Record<string, any> = any
> {
$set(props?: Partial<Props>): void;
$on<K extends Extract<keyof Events, string>>(type: K, callback: (e: Events[K]) => void): () => void;
$destroy(): void; $destroy(): void;
[accessor: string]: any; [accessor: string]: any;
} }
export class SvelteComponentDev extends SvelteComponent { export class SvelteComponentDev<
Props extends Record<string, any> = any,
Events extends Record<string, any> = any,
Slots extends Record<string, any> = any
> extends SvelteComponent<Props, Events> {
/**
* @private
* For type checking capabilities only.
* Does not exist at runtime.
* ### DO NOT USE!
*/
$$prop_def: Props;
/**
* @private
* For type checking capabilities only.
* Does not exist at runtime.
* ### DO NOT USE!
*/
$$events_def: Events;
/**
* @private
* For type checking capabilities only.
* Does not exist at runtime.
* ### DO NOT USE!
*/
$$slot_def: Slots;
constructor(options: { constructor(options: {
target: Element; target: Element;
anchor?: Element; anchor?: Element;
@ -113,7 +142,7 @@ export class SvelteComponentDev extends SvelteComponent {
hydrate?: boolean; hydrate?: boolean;
intro?: boolean; intro?: boolean;
$$inline?: boolean; $$inline?: boolean;
}) { }) {
if (!options || (!options.target && !options.$$inline)) { if (!options || (!options.target && !options.$$inline)) {
throw new Error("'target' is a required option"); throw new Error("'target' is a required option");
} }

Loading…
Cancel
Save