Skip to content

Commit

Permalink
fix(language-core): correct type narrowing from script to template (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
KazariEX authored Aug 25, 2024
1 parent ebc8710 commit fc22bf7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
9 changes: 6 additions & 3 deletions packages/language-core/lib/codegen/script/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ export function* generateTemplate(

if (!options.vueCompilerOptions.skipTemplateCodegen) {
if (isClassComponent) {
yield `__VLS_template() {${newLine}`;
yield `__VLS_template = (() => {${newLine}`;
}
else {
yield `function __VLS_template() {${newLine}`;
yield `const __VLS_template = (() => {${newLine}`;
}
const templateCodegenCtx = createTemplateCodegenContext(new Set());
yield `const __VLS_template_return = () => {${newLine}`;
yield* generateCtx(options, isClassComponent);
yield* generateTemplateContext(options, templateCodegenCtx);
yield* generateExportOptions(options);
yield* generateConstNameOption(options);
yield `}${endOfLine}`;
yield* generateInternalComponent(options, ctx, templateCodegenCtx);
yield `}${newLine}`;
yield `return __VLS_template_return${endOfLine}`;
yield `})()${endOfLine}`;
}
else {
yield `function __VLS_template() {${newLine}`;
Expand Down
8 changes: 4 additions & 4 deletions packages/tsc/tests/__snapshots__/dts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ export {};
`;
exports[`vue-tsc-dts > Input: template-slots/component.vue, Output: template-slots/component.vue.d.ts 1`] = `
"declare function __VLS_template(): {
"declare const __VLS_template: () => {
slots: {
"no-bind"?(_: {}): any;
default?(_: {
Expand Down Expand Up @@ -687,7 +687,7 @@ type __VLS_WithTemplateSlots<T, S> = T & {
exports[`vue-tsc-dts > Input: template-slots/component-define-slots.vue, Output: template-slots/component-define-slots.vue.d.ts 1`] = `
"import { VNode } from 'vue';
declare function __VLS_template(): {
declare const __VLS_template: () => {
slots: Readonly<{
default: (props: {
num: number;
Expand Down Expand Up @@ -729,7 +729,7 @@ type __VLS_WithTemplateSlots<T, S> = T & {
`;
exports[`vue-tsc-dts > Input: template-slots/component-destructuring.vue, Output: template-slots/component-destructuring.vue.d.ts 1`] = `
"declare function __VLS_template(): {
"declare const __VLS_template: () => {
slots: Readonly<{
bottom: (props: {
num: number;
Expand All @@ -755,7 +755,7 @@ type __VLS_WithTemplateSlots<T, S> = T & {
`;
exports[`vue-tsc-dts > Input: template-slots/component-no-script.vue, Output: template-slots/component-no-script.vue.d.ts 1`] = `
"declare function __VLS_template(): {
"declare const __VLS_template: () => {
slots: {
"no-bind"?(_: {}): any;
default?(_: {
Expand Down
5 changes: 5 additions & 0 deletions test-workspace/tsc/passedFixtures/vue3/#4391/comp.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script lang="ts" setup>
defineProps<{
foo: number;
}>();
</script>
12 changes: 12 additions & 0 deletions test-workspace/tsc/passedFixtures/vue3/#4391/main.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script lang="ts" setup>
import Comp from './comp.vue';
let value!: number | undefined;
if (!value) {
throw new Error('undefined');
}
</script>

<template>
<Comp :foo="value"></Comp>
</template>

0 comments on commit fc22bf7

Please sign in to comment.