Skip to content

Commit 1e408b8

Browse files
committed
fix(language-core): only generate the props it needs in generic components
1 parent 3b19a6e commit 1e408b8

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

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

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function* generateScriptSetup(
6666
}
6767

6868
yield `return {} as {${newLine}`
69-
+ ` props: ${ctx.localTypes.PrettifyLocal}<typeof __VLS_functionalComponentProps & __VLS_TemplateResult['attrs'] & __VLS_PublicProps> & __VLS_BuiltInPublicProps,${newLine}`
69+
+ ` props: ${ctx.localTypes.PrettifyLocal}<__VLS_OwnProps & __VLS_PublicProps & __VLS_TemplateResult['attrs']> & __VLS_BuiltInPublicProps,${newLine}`
7070
+ ` expose(exposed: import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<${scriptSetupRanges.defineExpose ? 'typeof __VLS_exposed' : '{}'}>): void,${newLine}`
7171
+ ` attrs: any,${newLine}`
7272
+ ` slots: __VLS_TemplateResult['slots'],${newLine}`
@@ -368,37 +368,39 @@ function* generateComponentProps(
368368
scriptSetup: NonNullable<Sfc['scriptSetup']>,
369369
scriptSetupRanges: ScriptSetupRanges
370370
): Generator<Code> {
371-
yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`;
372-
373-
if (scriptSetupRanges.defineProps?.arg) {
374-
yield `props: `;
375-
yield generateSfcBlockSection(
376-
scriptSetup,
377-
scriptSetupRanges.defineProps.arg.start,
378-
scriptSetupRanges.defineProps.arg.end,
379-
codeFeatures.navigation
380-
);
381-
yield `,${newLine}`;
371+
if (scriptSetup.generic) {
372+
yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`;
373+
374+
if (scriptSetupRanges.defineProps?.arg) {
375+
yield `props: `;
376+
yield generateSfcBlockSection(
377+
scriptSetup,
378+
scriptSetupRanges.defineProps.arg.start,
379+
scriptSetupRanges.defineProps.arg.end,
380+
codeFeatures.navigation
381+
);
382+
yield `,${newLine}`;
383+
}
384+
385+
yield* generateEmitsOption(options, scriptSetupRanges);
386+
387+
yield `})${endOfLine}`;
388+
389+
yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4
390+
? `import('${options.vueCompilerOptions.lib}').PublicProps`
391+
: options.vueCompilerOptions.target >= 3.0
392+
? `import('${options.vueCompilerOptions.lib}').VNodeProps`
393+
+ ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps`
394+
+ ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps`
395+
: `globalThis.JSX.IntrinsicAttributes`
396+
}`;
397+
yield endOfLine;
398+
399+
yield `type __VLS_OwnProps = `;
400+
yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}<InstanceType<typeof __VLS_fnComponent>['$props'], keyof __VLS_BuiltInPublicProps>`;
401+
yield endOfLine;
382402
}
383403

384-
yield* generateEmitsOption(options, scriptSetupRanges);
385-
386-
yield `})${endOfLine}`;
387-
388-
yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4
389-
? `import('${options.vueCompilerOptions.lib}').PublicProps`
390-
: options.vueCompilerOptions.target >= 3.0
391-
? `import('${options.vueCompilerOptions.lib}').VNodeProps`
392-
+ ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps`
393-
+ ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps`
394-
: `globalThis.JSX.IntrinsicAttributes`
395-
}`;
396-
yield endOfLine;
397-
398-
yield `let __VLS_functionalComponentProps!: `;
399-
yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}<InstanceType<typeof __VLS_fnComponent>['$props'], keyof __VLS_BuiltInPublicProps>`;
400-
yield endOfLine;
401-
402404
if (scriptSetupRanges.defineProp.length) {
403405
yield `const __VLS_defaults = {${newLine}`;
404406
for (const defineProp of scriptSetupRanges.defineProp) {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ exports[`vue-tsc-dts > Input: #4577/main.vue, Output: #4577/main.vue.d.ts 1`] =
55
value: string;
66
};
77
declare const _default: <Row extends BaseRow>(__VLS_props: NonNullable<Awaited<typeof __VLS_setup>>["props"], __VLS_ctx?: __VLS_PrettifyLocal<Pick<NonNullable<Awaited<typeof __VLS_setup>>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
8-
props: __VLS_PrettifyLocal<Pick<Partial<{}> & Omit<{} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, never> & Partial<{}> & {
8+
props: __VLS_PrettifyLocal<Pick<Partial<{}> & Omit<{} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, never> & {
99
nonGeneric: string;
1010
rows: Row[];
11-
}> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
11+
} & Partial<{}>> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
1212
expose(exposed: import("vue").ShallowUnwrapRef<{}>): void;
1313
attrs: any;
1414
slots: {
@@ -69,7 +69,7 @@ exports[`vue-tsc-dts > Input: events/component-generic.vue, Output: events/compo
6969
"declare const _default: <T>(__VLS_props: NonNullable<Awaited<typeof __VLS_setup>>["props"], __VLS_ctx?: __VLS_PrettifyLocal<Pick<NonNullable<Awaited<typeof __VLS_setup>>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
7070
props: __VLS_PrettifyLocal<Pick<Partial<{}> & Omit<{
7171
readonly onFoo?: (value: string) => any;
72-
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onFoo"> & Partial<{}> & {}> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
72+
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onFoo"> & {} & Partial<{}>> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
7373
expose(exposed: import("vue").ShallowUnwrapRef<{}>): void;
7474
attrs: any;
7575
slots: {};
@@ -89,11 +89,11 @@ exports[`vue-tsc-dts > Input: generic/component.vue, Output: generic/component.v
8989
props: __VLS_PrettifyLocal<Pick<Partial<{}> & Omit<{
9090
readonly "onUpdate:title"?: (value: string) => any;
9191
readonly onBar?: (data: number) => any;
92-
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onUpdate:title" | "onBar"> & Partial<{}> & ({
92+
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onUpdate:title" | "onBar"> & ({
9393
title?: string;
9494
} & {
9595
foo: number;
96-
})> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
96+
}) & Partial<{}>> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
9797
expose(exposed: import("vue").ShallowUnwrapRef<{
9898
baz: number;
9999
}>): void;
@@ -123,11 +123,11 @@ exports[`vue-tsc-dts > Input: generic/custom-extension-component.cext, Output: g
123123
props: __VLS_PrettifyLocal<Pick<Partial<{}> & Omit<{
124124
readonly "onUpdate:title"?: (value: string) => any;
125125
readonly onBar?: (data: number) => any;
126-
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onUpdate:title" | "onBar"> & Partial<{}> & ({
126+
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onUpdate:title" | "onBar"> & ({
127127
title?: string;
128128
} & {
129129
foo: number;
130-
})> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
130+
}) & Partial<{}>> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
131131
expose(exposed: import("vue").ShallowUnwrapRef<{
132132
baz: number;
133133
}>): void;

0 commit comments

Comments
 (0)