Skip to content

Commit fc22bf7

Browse files
authored
fix(language-core): correct type narrowing from script to template (#4689)
1 parent ebc8710 commit fc22bf7

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@ export function* generateTemplate(
1818

1919
if (!options.vueCompilerOptions.skipTemplateCodegen) {
2020
if (isClassComponent) {
21-
yield `__VLS_template() {${newLine}`;
21+
yield `__VLS_template = (() => {${newLine}`;
2222
}
2323
else {
24-
yield `function __VLS_template() {${newLine}`;
24+
yield `const __VLS_template = (() => {${newLine}`;
2525
}
2626
const templateCodegenCtx = createTemplateCodegenContext(new Set());
27+
yield `const __VLS_template_return = () => {${newLine}`;
2728
yield* generateCtx(options, isClassComponent);
2829
yield* generateTemplateContext(options, templateCodegenCtx);
2930
yield* generateExportOptions(options);
3031
yield* generateConstNameOption(options);
32+
yield `}${endOfLine}`;
3133
yield* generateInternalComponent(options, ctx, templateCodegenCtx);
32-
yield `}${newLine}`;
34+
yield `return __VLS_template_return${endOfLine}`;
35+
yield `})()${endOfLine}`;
3336
}
3437
else {
3538
yield `function __VLS_template() {${newLine}`;

packages/tsc/tests/__snapshots__/dts.spec.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ export {};
656656
`;
657657
658658
exports[`vue-tsc-dts > Input: template-slots/component.vue, Output: template-slots/component.vue.d.ts 1`] = `
659-
"declare function __VLS_template(): {
659+
"declare const __VLS_template: () => {
660660
slots: {
661661
"no-bind"?(_: {}): any;
662662
default?(_: {
@@ -687,7 +687,7 @@ type __VLS_WithTemplateSlots<T, S> = T & {
687687
688688
exports[`vue-tsc-dts > Input: template-slots/component-define-slots.vue, Output: template-slots/component-define-slots.vue.d.ts 1`] = `
689689
"import { VNode } from 'vue';
690-
declare function __VLS_template(): {
690+
declare const __VLS_template: () => {
691691
slots: Readonly<{
692692
default: (props: {
693693
num: number;
@@ -729,7 +729,7 @@ type __VLS_WithTemplateSlots<T, S> = T & {
729729
`;
730730
731731
exports[`vue-tsc-dts > Input: template-slots/component-destructuring.vue, Output: template-slots/component-destructuring.vue.d.ts 1`] = `
732-
"declare function __VLS_template(): {
732+
"declare const __VLS_template: () => {
733733
slots: Readonly<{
734734
bottom: (props: {
735735
num: number;
@@ -755,7 +755,7 @@ type __VLS_WithTemplateSlots<T, S> = T & {
755755
`;
756756
757757
exports[`vue-tsc-dts > Input: template-slots/component-no-script.vue, Output: template-slots/component-no-script.vue.d.ts 1`] = `
758-
"declare function __VLS_template(): {
758+
"declare const __VLS_template: () => {
759759
slots: {
760760
"no-bind"?(_: {}): any;
761761
default?(_: {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script lang="ts" setup>
2+
defineProps<{
3+
foo: number;
4+
}>();
5+
</script>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script lang="ts" setup>
2+
import Comp from './comp.vue';
3+
4+
let value!: number | undefined;
5+
if (!value) {
6+
throw new Error('undefined');
7+
}
8+
</script>
9+
10+
<template>
11+
<Comp :foo="value"></Comp>
12+
</template>

0 commit comments

Comments
 (0)