Skip to content

Commit 831ec04

Browse files
committed
feat: emits to props and drop internal component for generic
1 parent 6f87f71 commit 831ec04

File tree

6 files changed

+46
-73
lines changed

6 files changed

+46
-73
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ export function generateGlobalTypes({
126126
}
127127
>
128128
>;
129+
type __VLS_EmitsToProps<T> = __VLS_PrettifyGlobal<{
130+
[K in string & keyof T as \`on\${Capitalize<K>}\`]?:
131+
(...args: T[K] extends (...args: infer P) => any ? P : T[K] extends null ? any[] : never) => any;
132+
}>;
129133
type __VLS_ResolveEmits<
130134
Comp,
131135
Emits,

packages/language-core/lib/codegen/localTypes.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ import { endOfLine } from './utils';
55
export function getLocalTypesGenerator(vueCompilerOptions: VueCompilerOptions) {
66
const used = new Set<string>();
77

8-
const OmitKeepDiscriminatedUnion = defineHelper(
9-
`__VLS_OmitKeepDiscriminatedUnion`,
10-
() =>
11-
`
12-
type __VLS_OmitKeepDiscriminatedUnion<T, K extends keyof any> = T extends any
13-
? Pick<T, Exclude<keyof T, K>>
14-
: never;
15-
`.trimStart(),
16-
);
178
const WithDefaults = defineHelper(
189
`__VLS_WithDefaults`,
1910
() =>
@@ -78,7 +69,6 @@ type __VLS_TypePropsToOption<T> = {
7869
);
7970
const helpers = {
8071
[PrettifyLocal.name]: PrettifyLocal,
81-
[OmitKeepDiscriminatedUnion.name]: OmitKeepDiscriminatedUnion,
8272
[WithDefaults.name]: WithDefaults,
8373
[WithSlots.name]: WithSlots,
8474
[PropsChildren.name]: PropsChildren,
@@ -95,9 +85,6 @@ type __VLS_TypePropsToOption<T> = {
9585
get PrettifyLocal() {
9686
return PrettifyLocal.name;
9787
},
98-
get OmitKeepDiscriminatedUnion() {
99-
return OmitKeepDiscriminatedUnion.name;
100-
},
10188
get WithDefaults() {
10289
return WithDefaults.name;
10390
},

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

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { codeFeatures } from '../codeFeatures';
55
import { endOfLine, generateSfcBlockSection, identifierRegex, newLine } from '../utils';
66
import { generateCamelized } from '../utils/camelized';
77
import { wrapWith } from '../utils/wrapWith';
8-
import { generateComponent, generateEmitsOption } from './component';
8+
import { generateComponent } from './component';
99
import type { ScriptCodegenContext } from './context';
1010
import { generateConstExport, generateScriptSectionPartiallyEnding, type ScriptCodegenOptions } from './index';
1111
import { generateTemplate } from './template';
@@ -64,7 +64,19 @@ export function* generateScriptSetup(
6464
}
6565

6666
yield `return {} as {${newLine}`
67-
+ ` props: ${ctx.localTypes.PrettifyLocal}<__VLS_OwnProps & __VLS_PublicProps & Partial<__VLS_InheritedAttrs>> & __VLS_BuiltInPublicProps,${newLine}`
67+
+ ` props: ${ctx.localTypes.PrettifyLocal}<${
68+
scriptSetupRanges.defineEmits || scriptSetupRanges.defineModel.length
69+
? `__VLS_EmitProps & `
70+
: ``
71+
}__VLS_PublicProps & Partial<__VLS_InheritedAttrs>> & ${
72+
options.vueCompilerOptions.target >= 3.4
73+
? `import('${options.vueCompilerOptions.lib}').PublicProps`
74+
: options.vueCompilerOptions.target >= 3
75+
? `import('${options.vueCompilerOptions.lib}').VNodeProps`
76+
+ ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps`
77+
+ ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps`
78+
: `globalThis.JSX.IntrinsicAttributes`
79+
},${newLine}`
6880
+ ` expose(exposed: import('${options.vueCompilerOptions.lib}').ShallowUnwrapRef<${
6981
scriptSetupRanges.defineExpose ? 'typeof __VLS_exposed' : '{}'
7082
}>): void,${newLine}`
@@ -424,40 +436,6 @@ function* generateComponentProps(
424436
scriptSetup: NonNullable<Sfc['scriptSetup']>,
425437
scriptSetupRanges: ScriptSetupRanges,
426438
): Generator<Code> {
427-
if (scriptSetup.generic) {
428-
yield `const __VLS_fnComponent = (await import('${options.vueCompilerOptions.lib}')).defineComponent({${newLine}`;
429-
430-
if (scriptSetupRanges.defineProps?.arg) {
431-
yield `props: `;
432-
yield generateSfcBlockSection(
433-
scriptSetup,
434-
scriptSetupRanges.defineProps.arg.start,
435-
scriptSetupRanges.defineProps.arg.end,
436-
codeFeatures.navigation,
437-
);
438-
yield `,${newLine}`;
439-
}
440-
441-
yield* generateEmitsOption(options, scriptSetupRanges);
442-
443-
yield `})${endOfLine}`;
444-
445-
yield `type __VLS_BuiltInPublicProps = ${
446-
options.vueCompilerOptions.target >= 3.4
447-
? `import('${options.vueCompilerOptions.lib}').PublicProps`
448-
: options.vueCompilerOptions.target >= 3
449-
? `import('${options.vueCompilerOptions.lib}').VNodeProps`
450-
+ ` & import('${options.vueCompilerOptions.lib}').AllowedComponentProps`
451-
+ ` & import('${options.vueCompilerOptions.lib}').ComponentCustomProps`
452-
: `globalThis.JSX.IntrinsicAttributes`
453-
}`;
454-
yield endOfLine;
455-
456-
yield `type __VLS_OwnProps = `;
457-
yield `${ctx.localTypes.OmitKeepDiscriminatedUnion}<InstanceType<typeof __VLS_fnComponent>['$props'], keyof __VLS_BuiltInPublicProps>`;
458-
yield endOfLine;
459-
}
460-
461439
if (scriptSetupRanges.defineModel.length) {
462440
yield `const __VLS_defaults = {${newLine}`;
463441
for (const defineModel of scriptSetupRanges.defineModel) {

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,6 @@ function* generateTemplateCtx(
5555
exps.push(`{} as __VLS_StyleModules`);
5656
}
5757

58-
if (ctx.generatedPropsType || options.scriptSetupRanges?.defineProps) {
59-
yield `type __VLS_InternalProps = `;
60-
const { defineProps } = options.scriptSetupRanges ?? {};
61-
if (defineProps) {
62-
yield `__VLS_SpreadMerge<__VLS_PublicProps, typeof ${defineProps.name ?? `__VLS_props`}>`;
63-
}
64-
else {
65-
yield `__VLS_PublicProps`;
66-
}
67-
yield endOfLine;
68-
exps.push(`{} as __VLS_InternalProps`);
69-
exps.push(`{} as { $props: __VLS_InternalProps }`);
70-
}
71-
7258
const emitTypes: Code[] = [];
7359
if (options.scriptSetupRanges?.defineEmits) {
7460
emitTypes.push(`typeof ${options.scriptSetupRanges.defineEmits.name ?? `__VLS_emit`}`);
@@ -77,9 +63,27 @@ function* generateTemplateCtx(
7763
emitTypes.push(`typeof __VLS_modelEmit`);
7864
}
7965
if (emitTypes.length) {
66+
yield `type __VLS_EmitProps = __VLS_EmitsToProps<__VLS_NormalizeEmits<${emitTypes.join(' & ')}>>${endOfLine};`;
8067
exps.push(`{} as { $emit: ${emitTypes.join(' & ')} }`);
8168
}
8269

70+
if (options.scriptSetupRanges?.defineProps || ctx.generatedPropsType || emitTypes.length) {
71+
yield `type __VLS_InternalProps =`;
72+
const { defineProps } = options.scriptSetupRanges ?? {};
73+
if (defineProps) {
74+
yield ` __VLS_SpreadMerge<__VLS_PublicProps, typeof ${defineProps.name ?? `__VLS_props`}>`;
75+
}
76+
else if (ctx.generatedPropsType) {
77+
yield ` __VLS_PublicProps`;
78+
}
79+
if (emitTypes.length) {
80+
yield ` & __VLS_EmitProps`;
81+
}
82+
yield endOfLine;
83+
exps.push(`{} as { $props: __VLS_InternalProps }`);
84+
exps.push(`{} as __VLS_InternalProps`);
85+
}
86+
8387
exps.push(`{} as __VLS_Bindings`);
8488

8589
yield `const __VLS_ctx = `;

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`vue-tsc-dts > Input: #4577/main.vue, Output: #4577/main.vue.d.ts 1`] =
55
value: string;
66
};
77
declare const __VLS_export: <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> & {
8+
props: __VLS_PrettifyLocal<{
99
nonGeneric: string;
1010
rows: Row[];
1111
} & Partial<{}>> & import("vue").PublicProps;
@@ -84,10 +84,10 @@ export default _default;
8484
8585
exports[`vue-tsc-dts > Input: generic/component.vue, Output: generic/component.vue.d.ts 1`] = `
8686
"declare const __VLS_export: <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<{
87-
props: __VLS_PrettifyLocal<Pick<Partial<{}> & Omit<{
88-
readonly "onUpdate:title"?: (value: string) => any;
89-
readonly onBar?: (data: number) => any;
90-
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onUpdate:title" | "onBar"> & ({
87+
props: __VLS_PrettifyLocal<{
88+
"onUpdate:title"?: (value: string) => any;
89+
onBar?: (data: number) => any;
90+
} & ({
9191
foo: number;
9292
} & {
9393
title?: string;
@@ -115,10 +115,10 @@ type __VLS_PrettifyLocal<T> = {
115115
116116
exports[`vue-tsc-dts > Input: generic/custom-extension-component.cext, Output: generic/custom-extension-component.cext.d.ts 1`] = `
117117
"declare const __VLS_export: <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<{
118-
props: __VLS_PrettifyLocal<Pick<Partial<{}> & Omit<{
119-
readonly "onUpdate:title"?: (value: string) => any;
120-
readonly onBar?: (data: number) => any;
121-
} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, "onUpdate:title" | "onBar"> & ({
118+
props: __VLS_PrettifyLocal<{
119+
"onUpdate:title"?: (value: string) => any;
120+
onBar?: (data: number) => any;
121+
} & ({
122122
foo: number;
123123
} & {
124124
title?: string;

test-workspace/tsc/passedFixtures/vue3/components/main.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ declare const ScriptSetupGenericExact: <T, >(
6060
_expose?: NonNullable<Awaited<typeof _setup>>['expose'],
6161
_setup?: Promise<{
6262
props: {
63-
readonly onBar?: ((data: T) => any) | undefined;
63+
onBar?: ((data: T) => any) | undefined;
6464
foo: T;
6565
} & import('vue').VNodeProps & import('vue').AllowedComponentProps & import('vue').ComponentCustomProps,
6666
attrs: any,

0 commit comments

Comments
 (0)