diff --git a/packages/language-core/lib/codegen/globalTypes.ts b/packages/language-core/lib/codegen/globalTypes.ts index 9908372d43..82dc53cbb9 100644 --- a/packages/language-core/lib/codegen/globalTypes.ts +++ b/packages/language-core/lib/codegen/globalTypes.ts @@ -47,7 +47,7 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates N1 extends keyof __VLS_GlobalComponents ? N1 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N1] } : N2 extends keyof __VLS_GlobalComponents ? N2 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N2] } : N3 extends keyof __VLS_GlobalComponents ? N3 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N3] } : - ${strictTemplates ? '{}' : '{ [K in N0]: unknown }'} + ${strictTemplates ? '{}' : '{ [K in N0]: unknown }'}; type __VLS_FunctionalComponentProps = '__ctx' extends keyof __VLS_PickNotAny ? K extends { __ctx?: { props?: infer P } } ? NonNullable

: never : T extends (props: infer P, ...args: any) => any ? P : @@ -59,6 +59,15 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates : true : false : false; + type __VLS_NormalizeComponentEvent = ( + __VLS_IsFunction extends true + ? Props + : __VLS_IsFunction extends true + ? { [K in onEvent]?: Events[Event] } + : __VLS_IsFunction extends true + ? { [K in onEvent]?: Events[CamelizedEvent] } + : Props + )${ strictTemplates ? '' : ' & Record' }; // fix https://github.com/vuejs/language-tools/issues/926 type __VLS_UnionToIntersection = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never; type __VLS_OverloadUnionInner = U & T extends (...args: infer A) => infer R diff --git a/packages/language-core/lib/codegen/template/element.ts b/packages/language-core/lib/codegen/template/element.ts index 62edd30bd5..6776fe8ddb 100644 --- a/packages/language-core/lib/codegen/template/element.ts +++ b/packages/language-core/lib/codegen/template/element.ts @@ -245,7 +245,7 @@ export function* generateComponent( } } - const usedComponentEventsVar = yield* generateElementEvents(options, ctx, node, var_functionalComponent, var_componentInstance, var_componentEmit, var_componentEvents); + const usedComponentEventsVar = yield* generateElementEvents(options, ctx, node, var_functionalComponent, var_componentInstance, var_componentEvents); if (usedComponentEventsVar) { ctx.usedComponentCtxVars.add(var_defineComponentCtx); yield `let ${var_componentEmit}!: typeof ${var_defineComponentCtx}.emit${endOfLine}`; diff --git a/packages/language-core/lib/codegen/template/elementEvents.ts b/packages/language-core/lib/codegen/template/elementEvents.ts index 498330f2a5..b8646dd7f0 100644 --- a/packages/language-core/lib/codegen/template/elementEvents.ts +++ b/packages/language-core/lib/codegen/template/elementEvents.ts @@ -15,7 +15,6 @@ export function* generateElementEvents( node: CompilerDOM.ElementNode, componentVar: string, componentInstanceVar: string, - emitVar: string, eventsVar: string ): Generator { let usedComponentEventsVar = false; @@ -34,30 +33,7 @@ export function* generateElementEvents( yield `let ${propsVar}!: __VLS_FunctionalComponentProps${endOfLine}`; } const originalPropName = camelize('on-' + prop.arg.loc.source); - const originalPropNameObjectKey = variableNameRegex.test(originalPropName) - ? originalPropName - : `'${originalPropName}'`; - yield `const ${ctx.getInternalVariable()}: `; - if (!options.vueCompilerOptions.strictTemplates) { - yield `Record & `; - } - yield `(${newLine}`; - yield `__VLS_IsFunction extends true${newLine}`; - yield `? typeof ${propsVar}${newLine}`; - yield `: __VLS_IsFunction extends true${newLine}`; - yield `? {${newLine}`; - yield `/**__VLS_emit,${emitVar},${prop.arg.loc.source}*/${newLine}`; - yield `${originalPropNameObjectKey}?: typeof ${eventsVar}['${prop.arg.loc.source}']${newLine}`; - yield `}${newLine}`; - if (prop.arg.loc.source !== camelize(prop.arg.loc.source)) { - yield `: __VLS_IsFunction extends true${newLine}`; - yield `? {${newLine}`; - yield `/**__VLS_emit,${emitVar},${camelize(prop.arg.loc.source)}*/${newLine}`; - yield `${originalPropNameObjectKey}?: typeof ${eventsVar}['${camelize(prop.arg.loc.source)}']${newLine}`; - yield `}${newLine}`; - } - yield `: typeof ${propsVar}${newLine}`; - yield `) = {${newLine}`; + yield `const ${ctx.getInternalVariable()}: __VLS_NormalizeComponentEvent = {${newLine}`; yield* generateEventArg(ctx, prop.arg, true); yield `: `; yield* generateEventExpression(options, ctx, prop);