@@ -351,11 +351,12 @@ function buildEventHandlerType(
351
351
return nativeEventHandlerType ;
352
352
}
353
353
if ( element . kind === "component" ) {
354
- // I think we need to give an expression to the function call in order to reason correctly,
355
- // but that's not currently supported.
356
- // In the first place, the `@typescript-eslint/parser` currently
357
- // cannot correctly parse the import types of `*.svelte`.
358
- // return `(1 as any as ${elementName}['$on'])('${eventName}', $$expr)`;
354
+ // `@typescript-eslint/parser` currently cannot parse `*.svelte` import types correctly.
355
+ // So if we try to do a correct type parsing, it's argument type will be `any`.
356
+ // A workaround is to inject the type directly, as `CustomEvent<any>` is better than `any`.
357
+
358
+ // const componentEvents = `import('svelte').ComponentEvents<${elementName}>`;
359
+ // return `(e:'${eventName}' extends keyof ${componentEvents}?${componentEvents}['${eventName}']:CustomEvent<any>)=>void`;
359
360
360
361
return `(e:CustomEvent<any>)=>void` ;
361
362
}
@@ -366,11 +367,12 @@ function buildEventHandlerType(
366
367
const attrName = `on:${ eventName } ` ;
367
368
const importSvelteHTMLElements =
368
369
"import('svelte/elements').SvelteHTMLElements" ;
370
+ const elementAttributesType = `${ importSvelteHTMLElements } ['${ elementName } ']` ;
369
371
return [
370
372
`'${ elementName } ' extends keyof ${ importSvelteHTMLElements } ` ,
371
373
`?(` ,
372
- `'${ attrName } ' extends keyof ${ importSvelteHTMLElements } [' ${ elementName } '] ` ,
373
- `?${ importSvelteHTMLElements } [' ${ elementName } '] ['${ attrName } ']` ,
374
+ `'${ attrName } ' extends keyof ${ elementAttributesType } ` ,
375
+ `?${ elementAttributesType } ['${ attrName } ']` ,
374
376
`:${ nativeEventHandlerType } ` ,
375
377
`)` ,
376
378
`:${ nativeEventHandlerType } ` ,
0 commit comments