diff --git a/packages/language-core/lib/codegen/script/globalTypes.ts b/packages/language-core/lib/codegen/script/globalTypes.ts index 923d971aa9..bc1751d08f 100644 --- a/packages/language-core/lib/codegen/script/globalTypes.ts +++ b/packages/language-core/lib/codegen/script/globalTypes.ts @@ -62,7 +62,10 @@ declare global { function __VLS_nonNullable(t: T): T extends null | undefined ? never : T; type __VLS_SelfComponent = string extends N ? {} : N extends string ? { [P in N]: C } : {}; - type __VLS_WithComponent = + type __VLS_WithComponent = + N1 extends keyof Ctx ? N1 extends N0 ? Pick : { [K in N0]: Ctx[N1] } : + N2 extends keyof Ctx ? N2 extends N0 ? Pick : { [K in N0]: Ctx[N2] } : + N3 extends keyof Ctx ? N3 extends N0 ? Pick : { [K in N0]: Ctx[N3] } : N1 extends keyof LocalComponents ? N1 extends N0 ? Pick : { [K in N0]: LocalComponents[N1] } : N2 extends keyof LocalComponents ? N2 extends N0 ? Pick : { [K in N0]: LocalComponents[N2] } : N3 extends keyof LocalComponents ? N3 extends N0 ? Pick : { [K in N0]: LocalComponents[N3] } : diff --git a/packages/language-core/lib/codegen/template/element.ts b/packages/language-core/lib/codegen/template/element.ts index ff14fe34ff..f189471e7a 100644 --- a/packages/language-core/lib/codegen/template/element.ts +++ b/packages/language-core/lib/codegen/template/element.ts @@ -134,13 +134,8 @@ export function* generateComponent( } else if (!isComponentTag) { yield `// @ts-ignore${newLine}`; - yield `const ${var_originalComponent} = ({} as `; - for (const componentName of possibleOriginalNames) { - yield `'${componentName}' extends keyof typeof __VLS_ctx ? { '${getCanonicalComponentName(node.tag)}': typeof __VLS_ctx`; - yield* generatePropertyAccess(options, ctx, componentName); - yield ` }: `; - } - yield `typeof __VLS_resolvedLocalAndGlobalComponents)${newLine}`; + yield `const ${var_originalComponent} = __VLS_nonNullable(__VLS_resolvedLocalAndGlobalComponents`; + yield newLine; yield* generatePropertyAccess( options, ctx, @@ -148,7 +143,7 @@ export function* generateComponent( startTagOffset, ctx.codeFeatures.verification ); - yield endOfLine; + yield `)${endOfLine}`; // hover support for (const offset of tagOffsets) { diff --git a/packages/language-core/lib/codegen/template/index.ts b/packages/language-core/lib/codegen/template/index.ts index c78716c3b3..762a5e6736 100644 --- a/packages/language-core/lib/codegen/template/index.ts +++ b/packages/language-core/lib/codegen/template/index.ts @@ -117,13 +117,19 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator { yield `let __VLS_resolvedLocalAndGlobalComponents!: {}`; if (options.template.ast) { + const components = new Set(); for (const node of forEachElementNode(options.template.ast)) { if ( node.tagType === CompilerDOM.ElementTypes.COMPONENT && node.tag.toLowerCase() !== 'component' && !node.tag.includes('.') // namespace tag ) { - yield ` & __VLS_WithComponent<'${getCanonicalComponentName(node.tag)}', typeof __VLS_localComponents, `; + if (components.has(node.tag)) { + continue; + } + components.add(node.tag); + yield newLine; + yield ` & __VLS_WithComponent<'${getCanonicalComponentName(node.tag)}', typeof __VLS_ctx, typeof __VLS_localComponents, `; yield getPossibleOriginalComponentNames(node.tag, false) .map(name => `"${name}"`) .join(', ');