Skip to content

Commit

Permalink
typed SvelteComponent(Dev) interface (sveltejs#5431)
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm authored and taylorzane committed Dec 17, 2020
1 parent 9a02db7 commit 13b5b99
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## 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))
* 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))
Expand Down
11 changes: 7 additions & 4 deletions src/runtime/internal/Component.ts
Expand Up @@ -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$$;
$$set?: ($$props: any) => void;
$$set?: ($$props: Partial<Props>) => void;

$destroy() {
destroy_component(this, 1);
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] = []));
callbacks.push(callback);

Expand All @@ -231,7 +234,7 @@ export class SvelteComponent {
};
}

$set($$props) {
$set($$props: Partial<Props>) {
if (this.$$set && !is_empty($$props)) {
this.$$.skip_bound = true;
this.$$set($$props);
Expand Down
41 changes: 35 additions & 6 deletions src/runtime/internal/dev.ts
Expand Up @@ -97,23 +97,52 @@ export function validate_slots(name, slot, keys) {
}
}

type Props = Record<string, any>;
export interface SvelteComponentDev {
$set(props?: Props): void;
$on<T = any>(event: string, callback: (event: CustomEvent<T>) => void): () => void;
export interface SvelteComponentDev<
Props extends Record<string, any> = any,
Events extends Record<string, any> = any,
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;
[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: {
target: Element;
anchor?: Element;
props?: Props;
hydrate?: boolean;
intro?: boolean;
$$inline?: boolean;
}) {
}) {
if (!options || (!options.target && !options.$$inline)) {
throw new Error("'target' is a required option");
}
Expand Down

0 comments on commit 13b5b99

Please sign in to comment.