Skip to content

Commit 40dd226

Browse files
feat(language-core): narrowing components and directives types (#5841)
1 parent 1f940ad commit 40dd226

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export function* generateTemplate(
1313
): Generator<Code> {
1414
yield* generateSetupExposed(options, ctx);
1515
yield* generateTemplateCtx(options, ctx, selfType);
16-
yield* generateTemplateComponents(options);
17-
yield* generateTemplateDirectives(options);
16+
yield* generateTemplateComponents(options, ctx);
17+
yield* generateTemplateDirectives(options, ctx);
1818

1919
if (options.styleCodegen) {
2020
yield* options.styleCodegen.codes;
@@ -82,14 +82,20 @@ function* generateTemplateCtx(
8282
yield endOfLine;
8383
}
8484

85-
function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<Code> {
86-
const types: string[] = [`typeof ${names.ctx}`];
85+
function* generateTemplateComponents(
86+
{ script, scriptRanges }: ScriptCodegenOptions,
87+
ctx: ScriptCodegenContext,
88+
): Generator<Code> {
89+
const types: string[] = [];
8790

88-
if (options.script && options.scriptRanges?.componentOptions?.components) {
89-
const { components } = options.scriptRanges.componentOptions;
91+
if (ctx.generatedTypes.has(names.SetupExposed)) {
92+
types.push(names.SetupExposed);
93+
}
94+
if (script && scriptRanges?.componentOptions?.components) {
95+
const { components } = scriptRanges.componentOptions;
9096
yield `const __VLS_componentsOption = `;
9197
yield* generateSfcBlockSection(
92-
options.script,
98+
script,
9399
components.start,
94100
components.end,
95101
codeFeatures.navigation,
@@ -98,18 +104,24 @@ function* generateTemplateComponents(options: ScriptCodegenOptions): Generator<C
98104
types.push(`typeof __VLS_componentsOption`);
99105
}
100106

101-
yield `type __VLS_LocalComponents = ${types.join(` & `)}${endOfLine}`;
107+
yield `type __VLS_LocalComponents = ${types.length ? types.join(` & `) : `{}`}${endOfLine}`;
102108
yield `let ${names.components}!: __VLS_LocalComponents & __VLS_GlobalComponents${endOfLine}`;
103109
}
104110

105-
function* generateTemplateDirectives(options: ScriptCodegenOptions): Generator<Code> {
106-
const types: string[] = [`typeof ${names.ctx}`];
111+
function* generateTemplateDirectives(
112+
{ script, scriptRanges }: ScriptCodegenOptions,
113+
ctx: ScriptCodegenContext,
114+
): Generator<Code> {
115+
const types: string[] = [];
107116

108-
if (options.script && options.scriptRanges?.componentOptions?.directives) {
109-
const { directives } = options.scriptRanges.componentOptions;
117+
if (ctx.generatedTypes.has(names.SetupExposed)) {
118+
types.push(names.SetupExposed);
119+
}
120+
if (script && scriptRanges?.componentOptions?.directives) {
121+
const { directives } = scriptRanges.componentOptions;
110122
yield `const __VLS_directivesOption = `;
111123
yield* generateSfcBlockSection(
112-
options.script,
124+
script,
113125
directives.start,
114126
directives.end,
115127
codeFeatures.navigation,
@@ -118,7 +130,7 @@ function* generateTemplateDirectives(options: ScriptCodegenOptions): Generator<C
118130
types.push(`__VLS_ResolveDirectives<typeof __VLS_directivesOption>`);
119131
}
120132

121-
yield `type __VLS_LocalDirectives = ${types.join(` & `)}${endOfLine}`;
133+
yield `type __VLS_LocalDirectives = ${types.length ? types.join(` & `) : `{}`}${endOfLine}`;
122134
yield `let ${names.directives}!: __VLS_LocalDirectives & __VLS_GlobalDirectives${endOfLine}`;
123135
}
124136

test-workspace/tsc/passedFixtures/vue3.4/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"../vue3/#4828",
1717
"../vue3/#4878",
1818
"../vue3/#5120",
19+
"../vue3/#5840",
1920
"../vue3/rootEl",
2021
"../vue3/templateRef",
2122
"../vue3/templateRef_native",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script setup lang="ts" generic="T extends number">
2+
import { useTemplateRef } from 'vue';
3+
4+
const props = defineProps<{ generic?: T }>();
5+
const CompRef = useTemplateRef('CompRef');
6+
7+
CompRef.value?.exposedFunction1();
8+
</script>
9+
10+
<template>
11+
<Comp ref="CompRef" />
12+
</template>

0 commit comments

Comments
 (0)