Skip to content

Commit 21205fb

Browse files
committed
fix(language-core): fallthrough attributes break component type when root tag type is unknown (#4729)
1 parent 5a9bb12 commit 21205fb

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

packages/language-core/lib/codegen/script/component.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,6 @@ export function* generatePropsOption(
128128
const optionExpCodes: Code[] = [];
129129
const typeOptionExpCodes: Code[] = [];
130130

131-
if (inheritAttrs && options.templateCodegen?.inheritedAttrVars.size && !hasEmitsOption) {
132-
optionExpCodes.push(`{} as ${ctx.helperTypes.TypePropsToOption.name}<__VLS_PickNotAny<${ctx.helperTypes.OmitIndexSignature.name}<ReturnType<typeof __VLS_template>['attrs']>, {}>>`);
133-
typeOptionExpCodes.push(`{} as ReturnType<typeof __VLS_template>['attrs']`);
134-
}
135131
if (ctx.generatedPropsType) {
136132
optionExpCodes.push([
137133
`{} as `,
@@ -141,11 +137,23 @@ export function* generatePropsOption(
141137
].join(''));
142138
typeOptionExpCodes.push(`{} as __VLS_PublicProps`);
143139
}
144-
145140
if (scriptSetupRanges.props.define?.arg) {
146141
const { arg } = scriptSetupRanges.props.define;
147142
optionExpCodes.push(generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.navigation));
148143
}
144+
if (inheritAttrs && options.templateCodegen?.inheritedAttrVars.size && !hasEmitsOption) {
145+
const attrsType = `ReturnType<typeof __VLS_template>['attrs']`;
146+
const propsType = `__VLS_PickNotAny<${ctx.helperTypes.OmitIndexSignature.name}<${attrsType}>, {}>`;
147+
const optionType = `${ctx.helperTypes.TypePropsToOption.name}<${propsType}>`;
148+
if (optionExpCodes.length) {
149+
optionExpCodes.unshift(`{} as ${optionType}`);
150+
}
151+
else {
152+
// workaround for https://github.com/vuejs/core/pull/7419
153+
optionExpCodes.unshift(`{} as keyof ${propsType} extends never ? never: ${optionType}`);
154+
}
155+
typeOptionExpCodes.unshift(`{} as ${attrsType}`);
156+
}
149157

150158
const useTypeOption = options.vueCompilerOptions.target >= 3.5 && typeOptionExpCodes.length;
151159
const useOption = (!useTypeOption || scriptSetupRanges.props.withDefaults) && optionExpCodes.length;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script setup lang="ts">
2+
declare const child: unknown;
3+
</script>
4+
5+
<template>
6+
<child />
7+
</template>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script setup lang="ts">
2+
import basic from './basic.vue';
3+
</script>
4+
5+
<template>
6+
<basic />
7+
</template>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script setup lang="ts">
2+
declare const child: unknown;
3+
</script>
4+
5+
<template>
6+
<child />
7+
</template>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script setup lang="ts">
2+
import basic from './basic.vue';
3+
</script>
4+
5+
<template>
6+
<basic />
7+
</template>

0 commit comments

Comments
 (0)