Skip to content

Commit 03da3af

Browse files
committed
fix
1 parent b70c0c0 commit 03da3af

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/parser/converts/attr.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,12 @@ function buildEventHandlerType(
351351
return nativeEventHandlerType;
352352
}
353353
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`;
359360

360361
return `(e:CustomEvent<any>)=>void`;
361362
}
@@ -366,11 +367,12 @@ function buildEventHandlerType(
366367
const attrName = `on:${eventName}`;
367368
const importSvelteHTMLElements =
368369
"import('svelte/elements').SvelteHTMLElements";
370+
const elementAttributesType = `${importSvelteHTMLElements}['${elementName}']`;
369371
return [
370372
`'${elementName}' extends keyof ${importSvelteHTMLElements}`,
371373
`?(`,
372-
`'${attrName}' extends keyof ${importSvelteHTMLElements}['${elementName}']`,
373-
`?${importSvelteHTMLElements}['${elementName}']['${attrName}']`,
374+
`'${attrName}' extends keyof ${elementAttributesType}`,
375+
`?${elementAttributesType}['${attrName}']`,
374376
`:${nativeEventHandlerType}`,
375377
`)`,
376378
`:${nativeEventHandlerType}`,

0 commit comments

Comments
 (0)