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

Typed SvelteComponent interface #5431

Merged
merged 5 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/runtime/internal/Component.ts
Original file line number Diff line number Diff line change
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> & Record<string, any>) => void;
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved

$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> & Record<string, any>) {
if (this.$$set && !is_empty($$props)) {
this.$$.skip_bound = true;
this.$$set($$props);
Expand Down
43 changes: 36 additions & 7 deletions src/runtime/internal/dev.ts
Original file line number Diff line number Diff line change
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> & Record<string, any>): 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;
props?: Props & Record<string, any>;
hydrate?: boolean;
intro?: boolean;
$$inline?: boolean;
}) {
}) {
if (!options || (!options.target && !options.$$inline)) {
throw new Error(`'target' is a required option`);
}
Expand Down