Skip to content

Commit c0ec60a

Browse files
committed
fix(language-core): fix compatibility of generic component virtual code with TS 5.5
close #4577
1 parent 592264c commit c0ec60a

File tree

4 files changed

+66
-17
lines changed

4 files changed

+66
-17
lines changed

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ declare global {
2020
? `import('${vueCompilerOptions.lib}').GlobalComponents`
2121
: `import('${vueCompilerOptions.lib}').GlobalComponents & Pick<typeof import('${vueCompilerOptions.lib}'), 'Transition' | 'TransitionGroup' | 'KeepAlive' | 'Suspense' | 'Teleport'>;`
2222
}
23-
type __VLS_BuiltInPublicProps = ${vueCompilerOptions.target >= 3.4
24-
? `import('${vueCompilerOptions.lib}').PublicProps;`
25-
: vueCompilerOptions.target >= 3.0
26-
? `import('${vueCompilerOptions.lib}').VNodeProps
27-
& import('${vueCompilerOptions.lib}').AllowedComponentProps
28-
& import('${vueCompilerOptions.lib}').ComponentCustomProps;`
29-
: `globalThis.JSX.IntrinsicAttributes;`
30-
31-
}
3223
type __VLS_IsAny<T> = 0 extends 1 & T ? true : false;
3324
type __VLS_PickNotAny<A, B> = __VLS_IsAny<A> extends true ? B : A;
3425

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export function* generateScriptSetup(
4242
yield `,`;
4343
}
4444
yield `>(${newLine}`
45-
+ ` __VLS_props: Awaited<typeof __VLS_setup>['props'],${newLine}`
46-
+ ` __VLS_ctx?: ${ctx.helperTypes.Prettify.name}<Pick<Awaited<typeof __VLS_setup>, 'attrs' | 'emit' | 'slots'>>,${newLine}` // use __VLS_Prettify for less dts code
45+
+ ` __VLS_props: NonNullable<Awaited<typeof __VLS_setup>>['props'],${newLine}`
46+
+ ` __VLS_ctx?: ${ctx.helperTypes.Prettify.name}<Pick<NonNullable<Awaited<typeof __VLS_setup>>, 'attrs' | 'emit' | 'slots'>>,${newLine}` // use __VLS_Prettify for less dts code
4747
+ ` __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>['expose'],${newLine}`
4848
+ ` __VLS_setup = (async () => {${newLine}`;
4949
yield* generateSetupFunction(options, ctx, scriptSetup, scriptSetupRanges, undefined, definePropMirrors);
@@ -267,6 +267,17 @@ function* generateComponentProps(
267267
}
268268
yield* generateEmitsOption(options, scriptSetup, scriptSetupRanges);
269269
yield `})${endOfLine}`;
270+
271+
yield `type __VLS_BuiltInPublicProps = ${options.vueCompilerOptions.target >= 3.4
272+
? `import('${options.vueCompilerOptions.lib}').PublicProps;`
273+
: options.vueCompilerOptions.target >= 3.0
274+
? `import('${options.vueCompilerOptions.lib}').VNodeProps
275+
& import('${options.vueCompilerOptions.lib}').AllowedComponentProps
276+
& import('${options.vueCompilerOptions.lib}').ComponentCustomProps;`
277+
: `globalThis.JSX.IntrinsicAttributes;`
278+
}`;
279+
yield endOfLine;
280+
270281
yield `let __VLS_functionalComponentProps!: `;
271282
yield `${ctx.helperTypes.OmitKeepDiscriminatedUnion.name}<InstanceType<typeof __VLS_fnComponent>['$props'], keyof __VLS_BuiltInPublicProps>`;
272283
yield endOfLine;

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

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`vue-tsc-dts > Input: #4577/main.vue, Output: #4577/main.vue.d.ts 1`] = `
4+
"export type BaseRow = {
5+
value: string;
6+
};
7+
declare const _default: <Row extends BaseRow>(__VLS_props: NonNullable<Awaited<typeof __VLS_setup>>["props"], __VLS_ctx?: __VLS_Prettify<Pick<NonNullable<Awaited<typeof __VLS_setup>>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
8+
props: __VLS_Prettify<Pick<Partial<{}> & Omit<{} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps & Readonly<import("vue").ExtractPropTypes<{}>>, never>, never> & {
9+
nonGeneric: string;
10+
rows: Row[];
11+
}> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
12+
expose(exposed: import("vue").ShallowUnwrapRef<{}>): void;
13+
attrs: any;
14+
slots: ReturnType<() => {
15+
default?(_: {
16+
row: Row;
17+
}): any;
18+
}>;
19+
emit: {};
20+
}>) => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
21+
[key: string]: any;
22+
}> & {
23+
__ctx?: Awaited<typeof __VLS_setup>;
24+
};
25+
export default _default;
26+
27+
type __VLS_Prettify<T> = {
28+
[K in keyof T]: T[K];
29+
} & {};
30+
"
31+
`;
32+
333
exports[`vue-tsc-dts > Input: class-slots/component.vue, Output: class-slots/component.vue.d.ts 1`] = `
434
"import { VNode } from 'vue';
535
declare const _default: new () => {
@@ -39,12 +69,12 @@ export default _default;
3969
`;
4070
4171
exports[`vue-tsc-dts > Input: events/component-generic.vue, Output: events/component-generic.vue.d.ts 1`] = `
42-
"declare const _default: <T>(__VLS_props: Awaited<typeof __VLS_setup>["props"], __VLS_ctx?: __VLS_Prettify<Pick<Awaited<typeof __VLS_setup>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
72+
"declare const _default: <T>(__VLS_props: NonNullable<Awaited<typeof __VLS_setup>>["props"], __VLS_ctx?: __VLS_Prettify<Pick<NonNullable<Awaited<typeof __VLS_setup>>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
4373
props: __VLS_Prettify<Pick<Partial<{}> & Omit<{
4474
onFoo?: (value: string) => any;
4575
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps & Readonly<import("vue").ExtractPropTypes<{}>> & {
4676
onFoo?: (value: string) => any;
47-
}, never>, "onFoo"> & {}> & __VLS_BuiltInPublicProps;
77+
}, never>, "onFoo"> & {}> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
4878
expose(exposed: import("vue").ShallowUnwrapRef<{}>): void;
4979
attrs: any;
5080
slots: ReturnType<() => {}>;
@@ -62,7 +92,7 @@ type __VLS_Prettify<T> = {
6292
`;
6393
6494
exports[`vue-tsc-dts > Input: generic/component.vue, Output: generic/component.vue.d.ts 1`] = `
65-
"declare const _default: <T>(__VLS_props: Awaited<typeof __VLS_setup>["props"], __VLS_ctx?: __VLS_Prettify<Pick<Awaited<typeof __VLS_setup>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
95+
"declare const _default: <T>(__VLS_props: NonNullable<Awaited<typeof __VLS_setup>>["props"], __VLS_ctx?: __VLS_Prettify<Pick<NonNullable<Awaited<typeof __VLS_setup>>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
6696
props: __VLS_Prettify<Pick<Partial<{}> & Omit<{
6797
"onUpdate:title"?: (title: string) => any;
6898
onBar?: (data: number) => any;
@@ -73,7 +103,7 @@ exports[`vue-tsc-dts > Input: generic/component.vue, Output: generic/component.v
73103
title?: string;
74104
} & {
75105
foo: number;
76-
})> & __VLS_BuiltInPublicProps;
106+
})> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
77107
expose(exposed: import("vue").ShallowUnwrapRef<{
78108
baz: number;
79109
}>): void;
@@ -101,7 +131,7 @@ type __VLS_Prettify<T> = {
101131
`;
102132
103133
exports[`vue-tsc-dts > Input: generic/custom-extension-component.cext, Output: generic/custom-extension-component.cext.d.ts 1`] = `
104-
"declare const _default: <T>(__VLS_props: Awaited<typeof __VLS_setup>["props"], __VLS_ctx?: __VLS_Prettify<Pick<Awaited<typeof __VLS_setup>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
134+
"declare const _default: <T>(__VLS_props: NonNullable<Awaited<typeof __VLS_setup>>["props"], __VLS_ctx?: __VLS_Prettify<Pick<NonNullable<Awaited<typeof __VLS_setup>>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
105135
props: __VLS_Prettify<Pick<Partial<{}> & Omit<{
106136
"onUpdate:title"?: (title: string) => any;
107137
onBar?: (data: number) => any;
@@ -112,7 +142,7 @@ exports[`vue-tsc-dts > Input: generic/custom-extension-component.cext, Output: g
112142
title?: string;
113143
} & {
114144
foo: number;
115-
})> & __VLS_BuiltInPublicProps;
145+
})> & (import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps);
116146
expose(exposed: import("vue").ShallowUnwrapRef<{
117147
baz: number;
118148
}>): void;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div>
3+
<div v-for="(row, index) in rows">
4+
<span>Row #{{ index }}</span>
5+
<slot :row="row" />
6+
</div>
7+
</div>
8+
</template>
9+
10+
<script setup lang="ts" generic="Row extends BaseRow">
11+
export type BaseRow = { value: string; };
12+
13+
defineProps<{
14+
nonGeneric: string;
15+
rows: Row[];
16+
}>();
17+
</script>

0 commit comments

Comments
 (0)