diff --git a/packages/svelte2tsx/src/svelte2tsx.ts b/packages/svelte2tsx/src/svelte2tsx.ts index 298b740ae..a9f3218c9 100644 --- a/packages/svelte2tsx/src/svelte2tsx.ts +++ b/packages/svelte2tsx/src/svelte2tsx.ts @@ -534,7 +534,7 @@ function processInstanceScriptContent(str: MagicString, script: Node): InstanceS if ( (ts.isPrefixUnaryExpression(parent) || ts.isPostfixUnaryExpression(parent)) && parent.operator !== - ts.SyntaxKind.ExclamationToken /* `!$store` does not need processing */ + ts.SyntaxKind.ExclamationToken /* `!$store` does not need processing */ ) { let simpleOperator: string; if (parent.operator === ts.SyntaxKind.PlusPlusToken) { @@ -854,18 +854,17 @@ function addComponentExport( // the prop is optional strictMode && isTsFile ? uses$$propsOr$$restProps - ? '__sveltets_with_any(render().props)' - : 'render().props' - : `__sveltets_partial${uses$$propsOr$$restProps ? '_with_any' : ''}(render().props)`; + ? '__sveltets_with_any(render)' + : 'render' + : `__sveltets_partial${uses$$propsOr$$restProps ? '_with_any' : ''}(render)`; const doc = formatComponentDocumentation(componentDocumentation); const statement = - `\n\n${doc}export default class ${ - className ? `${className} ` : '' - }{\n $$prop_def = ${propDef}\n $$slot_def = render().slots` + + `\n\n${doc}export default class${ + className ? ` ${className}` : '' + } extends createSvelte2TsxComponent(${propDef}) {` + createClassGetters(getters) + - `\n $on = __sveltets_eventDef(render().events)` + '\n}'; str.append(statement); @@ -949,7 +948,7 @@ function createRenderFunction({ const returnString = `\nreturn { props: ${exportedNames.createPropsStr( - isTsFile + isTsFile, )}, slots: ${slotsAsDef}, getters: ${createRenderFunctionGetterStr(getters)}` + `, events: ${eventMapToString(events)} }}`; @@ -1043,6 +1042,6 @@ export function svelte2tsx( return { code: str.toString(), map: str.generateMap({ hires: true, source: options?.filename }), - exportedNames + exportedNames, }; } diff --git a/packages/svelte2tsx/svelte-shims.d.ts b/packages/svelte2tsx/svelte-shims.d.ts index b2cbdc54b..3f6537201 100644 --- a/packages/svelte2tsx/svelte-shims.d.ts +++ b/packages/svelte2tsx/svelte-shims.d.ts @@ -1,10 +1,62 @@ declare module '*.svelte' { - export default class { - $$prop_def: any; - $$slot_def: any; + export default Svelte2TsxComponent +} - $on(event: string, handler: (e: Event) => any): () => void - } +declare class Svelte2TsxComponent< + Props extends {} = {}, + Events extends {} = {}, + Slots extends {} = {}, + StrictEvents extends boolean = true +> { + // svelte2tsx-specific + /** + * @internal This is for type checking capabilities only + * and does not exist at runtime. Don't use this property. + */ + $$prop_def: Props; + /** + * @internal This is for type checking capabilities only + * and does not exist at runtime. Don't use this property. + */ + $$slot_def: Slots; + // https://svelte.dev/docs#Client-side_component_API + constructor(options: { + /** + * An HTMLElement to render to. This option is required. + */ + target: Element; + /** + * A child of `target` to render the component immediately before. + */ + anchor?: Element; + /** + * An object of properties to supply to the component. + */ + props?: Props; + hydrate?: boolean; + intro?: boolean; + $$inline?: boolean; + }); + /** + * Causes the callback function to be called whenever the component dispatches an event. + * A function is returned that will remove the event listener when called. + */ + $on(event: K, handler: (e: SvelteExtractEvent) => any): void; + /** + * Causes the callback function to be called whenever the component dispatches an event. + * A function is returned that will remove the event listener when called. + */ + $on(event: StrictEvents extends true ? never : string, handler: (e: CustomEvent) => any): void; + /** + * Removes a component from the DOM and triggers any `onDestroy` handlers. + */ + $destroy(): void; + /** + * Programmatically sets props on an instance. + * `component.$set({ x: 1 })` is equivalent to `x = 1` inside the component's `