From 3c564b0cab263ee83ee7eac4a38dbe4384552cdb Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Wed, 26 Apr 2023 04:19:52 +0800 Subject: [PATCH] fix: cannot use generic components inside defineComponent close #2709 --- packages/vue-language-core/src/generators/script.ts | 4 ++-- packages/vue-language-core/src/utils/localTypes.ts | 7 +++---- packages/vue-test-workspace/vue-tsc/#2709/App.vue | 10 ++++++++++ packages/vue-test-workspace/vue-tsc/#2709/MyList.vue | 9 +++++++++ .../vue-test-workspace/vue-tsc/components/main.vue | 4 ++-- .../vue-test-workspace/vue-tsc/defineProp_B/main.vue | 4 ++-- 6 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 packages/vue-test-workspace/vue-tsc/#2709/App.vue create mode 100644 packages/vue-test-workspace/vue-tsc/#2709/MyList.vue diff --git a/packages/vue-language-core/src/generators/script.ts b/packages/vue-language-core/src/generators/script.ts index f4a94e541..d03951691 100644 --- a/packages/vue-language-core/src/generators/script.ts +++ b/packages/vue-language-core/src/generators/script.ts @@ -295,7 +295,7 @@ export function generate( codes.push(`>`); codes.push('(\n'); codes.push(`__VLS_props: typeof __VLS_setup['props'] & import('vue').VNodeProps,\n`); - codes.push(`__VLS_ctx?: Pick,\n`); + codes.push(`__VLS_ctx?: Pick,\n`); codes.push('__VLS_setup = (() => {\n'); scriptSetupGeneratedOffset = generateSetupFunction(true, 'none', definePropMirrors); @@ -411,7 +411,7 @@ export function generate( codes.push('emit: typeof __VLS_emit'); codes.push('};\n'); codes.push('})(),\n'); - codes.push(') => ({} as import("vue").VNode & { __props?: typeof __VLS_props, __ctx?: typeof __VLS_ctx }))'); + codes.push(') => ({} as import("vue").VNode & { __ctx?: typeof __VLS_setup }))'); } else if (!sfc.script) { // no script block, generate script setup code at root diff --git a/packages/vue-language-core/src/utils/localTypes.ts b/packages/vue-language-core/src/utils/localTypes.ts index a737d65ee..f66e51685 100644 --- a/packages/vue-language-core/src/utils/localTypes.ts +++ b/packages/vue-language-core/src/utils/localTypes.ts @@ -91,13 +91,12 @@ export declare function asFunctionalComponent any ? (props: (K extends { $props: infer Props } ? Props : any)${vueCompilerOptions.strictTemplates ? '' : ' & Record'}, ctx?: { attrs?: any, - expose?(exposed: K): void, slots?: K extends { ${getSlotsPropertyName(vueCompilerOptions.target)}: infer Slots } ? Slots : any, emit?: K extends { $emit: infer Emit } ? Emit : any - }) => JSX.Element & { __ctx?: typeof ctx, __props?: typeof props } + }) => JSX.Element & { __ctx?: typeof ctx & { props?: typeof props; expose?(exposed: K): void; } } : T extends () => any ? (props: {}, ctx?: any) => ReturnType : T extends (...args: any) => any ? T - : (_: T & Record, ctx?: any) => { __ctx?: { attrs?: unknown, expose?: unknown, slots?: unknown, emit?: unknown }, __props?: T & Record }; // IntrinsicElement + : (_: T & Record, ctx?: any) => { __ctx?: { attrs?: unknown, expose?: unknown, slots?: unknown, emit?: unknown, props?: T & Record } }; // IntrinsicElement declare function functionalComponentArgsRest any>(t: T): Parameters['length'] extends 2 ? [any] : []; export declare function pickEvent(emit: Emit, emitKey: K, event: E): FillingEventArg< PickNotAny< @@ -112,7 +111,7 @@ export declare function pickFunctionalComponentCtx(comp: T, compInstance: type AsFunctionOrAny = unknown extends F ? any : ((...args: any) => any) extends F ? F : any; export declare function componentProps(comp: T, fnReturn: K): - PickNotAny extends { __props: infer P } ? NonNullable

+ PickNotAny extends { __ctx: { props: infer P } } ? NonNullable

: T extends (props: infer P, ...args: any) => any ? NonNullable

: {}; `.trim(); diff --git a/packages/vue-test-workspace/vue-tsc/#2709/App.vue b/packages/vue-test-workspace/vue-tsc/#2709/App.vue new file mode 100644 index 000000000..a25572bd1 --- /dev/null +++ b/packages/vue-test-workspace/vue-tsc/#2709/App.vue @@ -0,0 +1,10 @@ + diff --git a/packages/vue-test-workspace/vue-tsc/#2709/MyList.vue b/packages/vue-test-workspace/vue-tsc/#2709/MyList.vue new file mode 100644 index 000000000..a0d41ea92 --- /dev/null +++ b/packages/vue-test-workspace/vue-tsc/#2709/MyList.vue @@ -0,0 +1,9 @@ + + + diff --git a/packages/vue-test-workspace/vue-tsc/components/main.vue b/packages/vue-test-workspace/vue-tsc/components/main.vue index b141200aa..8ac609500 100644 --- a/packages/vue-test-workspace/vue-tsc/components/main.vue +++ b/packages/vue-test-workspace/vue-tsc/components/main.vue @@ -65,7 +65,7 @@ const ScriptSetupDefaultPropsExact = defineComponent({ // vue 3.3 generic declare const ScriptSetupGenericExact: ( _props: import('vue').VNodeProps & NonNullable['props'], - _ctx?: Pick, 'expose' | 'attrs' | 'emit' | 'slots'>, + _ctx?: Pick, 'attrs' | 'emit' | 'slots'>, _setup?: { props: { foo: T } & { [K in keyof JSX.ElementChildrenAttribute]?: { default(data: T): any } }, attrs: any, @@ -73,7 +73,7 @@ declare const ScriptSetupGenericExact: ( emit: { (e: 'bar', data: T): void }, expose(_exposed: { baz: T }): void, } -) => import('vue').VNode & { __props?: typeof _props, __ctx?: typeof _ctx }; +) => import('vue').VNode & { __ctx?: typeof _setup }; exactType(ScriptSetup, ScriptSetupExact); exactType(ScriptSetupExpose, ScriptSetupExposeExact); diff --git a/packages/vue-test-workspace/vue-tsc/defineProp_B/main.vue b/packages/vue-test-workspace/vue-tsc/defineProp_B/main.vue index 56df36901..09729bc47 100644 --- a/packages/vue-test-workspace/vue-tsc/defineProp_B/main.vue +++ b/packages/vue-test-workspace/vue-tsc/defineProp_B/main.vue @@ -20,7 +20,7 @@ const ScriptSetupExact = defineComponent({ }); declare const ScriptSetupGenericExact: ( _props: import('vue').VNodeProps & NonNullable['props'], - _ctx?: Pick, 'expose' | 'attrs' | 'emit' | 'slots'>, + _ctx?: Pick, 'attrs' | 'emit' | 'slots'>, _setup?: { props: { a?: T | undefined; @@ -33,7 +33,7 @@ declare const ScriptSetupGenericExact: ( emit: any, expose(_exposed: {}): void, } -) => import('vue').VNode & { __props?: typeof _props, __ctx?: typeof _ctx }; +) => import('vue').VNode & { __ctx?: typeof _setup }; exactType(ScriptSetup, ScriptSetupExact); exactType(ScriptSetupGeneric, ScriptSetupGenericExact);