Skip to content

Commit

Permalink
fix: cannot infer parameter type in recursive component
Browse files Browse the repository at this point in the history
close #2140
  • Loading branch information
johnsoncodehk committed Nov 29, 2022
1 parent da8ea7d commit 9ae9323
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
28 changes: 14 additions & 14 deletions vue-language-tools/vue-language-core/src/generators/script.ts
Expand Up @@ -288,12 +288,12 @@ export function generate(

if (scriptRanges?.exportDefault && scriptRanges.exportDefault.expression.start !== scriptRanges.exportDefault.args.start) {
// use defineComponent() from user space code if it exist
codeGen.push(`const __VLS_Component = `);
codeGen.push(`const __VLS_publicComponent = `);
addVirtualCode('script', scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.args.start);
codeGen.push(`{\n`);
}
else {
codeGen.push(`const __VLS_Component = (await import('${vueLibName}')).defineComponent({\n`);
codeGen.push(`const __VLS_publicComponent = (await import('${vueLibName}')).defineComponent({\n`);
}

if (!bypassDefineComponent) {
Expand Down Expand Up @@ -397,20 +397,20 @@ export function generate(
writeTemplate();

if (vueCompilerOptions.experimentalRfc436) {
codeGen.push(`return {} as Omit<JSX.Element, 'props' | 'children'> & Omit<InstanceType<typeof __VLS_Component>, '$slots' | '$emit'>`);
codeGen.push(`return {} as Omit<JSX.Element, 'props' | 'children'> & Omit<InstanceType<typeof __VLS_publicComponent>, '$slots' | '$emit'>`);
codeGen.push(` & {\n`);
if (scriptSetupRanges.propsTypeArg) {
codeGen.push(`props: typeof __VLS_props,\n`);
}
else {
codeGen.push(`props: InstanceType<typeof __VLS_Component>['$props'],\n`);
codeGen.push(`props: InstanceType<typeof __VLS_publicComponent>['$props'],\n`);
}
codeGen.push(`$emit: `);
if (scriptSetupRanges.emitsTypeArg) {
addVirtualCode('scriptSetup', scriptSetupRanges.emitsTypeArg.start, scriptSetupRanges.emitsTypeArg.end);
}
else {
codeGen.push(`InstanceType<typeof __VLS_Component>['$emit']`);
codeGen.push(`InstanceType<typeof __VLS_publicComponent>['$emit']`);
}
codeGen.push(`,\n`);
if (htmlGen?.slotsNum) {
Expand All @@ -422,7 +422,7 @@ export function generate(
codeGen.push(`};\n`);
}
else {
codeGen.push(`return {} as typeof __VLS_Component`);
codeGen.push(`return {} as typeof __VLS_publicComponent`);
if (htmlGen?.slotsNum) {
codeGen.push(` & { new (): { $slots: ReturnType<typeof __VLS_template> } }`);
}
Expand Down Expand Up @@ -477,7 +477,7 @@ export function generate(

if (sfc.scriptSetup && scriptSetupRanges) {

codeGen.push(`const __VLS_component = (await import('${vueLibName}')).defineComponent({\n`);
codeGen.push(`const __VLS_internalComponent = (await import('${vueLibName}')).defineComponent({\n`);
codeGen.push(`setup() {\n`);
codeGen.push(`return {\n`);
// fill ctx from props
Expand Down Expand Up @@ -556,10 +556,10 @@ export function generate(
codeGen.push(`});\n`); // defineComponent({
}
else if (sfc.script) {
codeGen.push(`let __VLS_component!: typeof import('./${path.basename(fileName)}')['default'];\n`);
codeGen.push(`let __VLS_internalComponent!: typeof import('./${path.basename(fileName)}')['default'];\n`);
}
else {
codeGen.push(`const __VLS_component = (await import('${vueLibName}')).defineComponent({});\n`);
codeGen.push(`const __VLS_internalComponent = (await import('${vueLibName}')).defineComponent({});\n`);
}
}
function writeExportOptions() {
Expand Down Expand Up @@ -604,9 +604,9 @@ export function generate(
codeGen.push(`let __VLS_ctx!: ${useGlobalThisTypeInCtx ? 'typeof globalThis &' : ''}`);
codeGen.push(`import('./__VLS_types.js').PickNotAny<__VLS_Ctx, {}> & `);
if (sfc.scriptSetup) {
codeGen.push(`InstanceType<import('./__VLS_types.js').PickNotAny<typeof __VLS_Component, new () => {}>> & `);
codeGen.push(`InstanceType<import('./__VLS_types.js').PickNotAny<typeof __VLS_publicComponent, new () => {}>> & `);
}
codeGen.push(`InstanceType<import('./__VLS_types.js').PickNotAny<typeof __VLS_component, new () => {}>> & {\n`);
codeGen.push(`InstanceType<import('./__VLS_types.js').PickNotAny<typeof __VLS_internalComponent, new () => {}>> & {\n`);

/* CSS Module */
for (const cssModule of cssModuleClasses) {
Expand All @@ -626,10 +626,10 @@ export function generate(

/* Components */
codeGen.push('/* Components */\n');
codeGen.push(`let __VLS_localComponents!: NonNullable<typeof __VLS_component extends { components: infer C } ? C : {}> & typeof __VLS_componentsOption & typeof __VLS_ctx;\n`);
codeGen.push(`let __VLS_localComponents!: NonNullable<typeof __VLS_internalComponent extends { components: infer C } ? C : {}> & typeof __VLS_componentsOption & typeof __VLS_ctx;\n`);
codeGen.push(`let __VLS_otherComponents!: typeof __VLS_localComponents & Omit<import('./__VLS_types.js').GlobalComponents, keyof typeof __VLS_localComponents>;\n`);
codeGen.push(`let __VLS_selfComponent!: import('./__VLS_types.js').SelfComponent<typeof __VLS_name, typeof __VLS_component & (new () => { ${getSlotsPropertyName(vueCompilerOptions.target ?? 3)}: typeof __VLS_slots })>;\n`);
codeGen.push(`let __VLS_components!: typeof __VLS_otherComponents & Omit<typeof __VLS_selfComponent, keyof typeof __VLS_otherComponents>;\n`);
codeGen.push(`let __VLS_own!: import('./__VLS_types.js').SelfComponent<typeof __VLS_name, typeof __VLS_internalComponent & typeof __VLS_publicComponent & (new () => { ${getSlotsPropertyName(vueCompilerOptions.target ?? 3)}: typeof __VLS_slots })>;\n`);
codeGen.push(`let __VLS_components!: typeof __VLS_otherComponents & Omit<typeof __VLS_own, keyof typeof __VLS_otherComponents>;\n`);

/* Style Scoped */
codeGen.push('/* Style Scoped */\n');
Expand Down
Expand Up @@ -833,10 +833,10 @@ export function generate(
const _varComponentInstanceA = `__VLS_${elementIndex++}`;
const _varComponentInstanceB = `__VLS_${elementIndex++}`;
_varComponentInstance = `__VLS_${elementIndex++}`;
codeGen.push(`const ${_varComponentInstanceA} = new ${componentVar}({ `);
codeGen.push(`const ${_varComponentInstanceA} = new __VLS_templateComponents.${componentVar}({ `);
writeProps(node, 'class', 'slots');
codeGen.push(`});\n`);
codeGen.push(`const ${_varComponentInstanceB} = ${componentVar}({ `);
codeGen.push(`const ${_varComponentInstanceB} = __VLS_templateComponents.${componentVar}({ `);
writeProps(node, 'class', 'slots');
codeGen.push(`});\n`);
codeGen.push(`let ${_varComponentInstance}!: import('./__VLS_types.js').PickNotAny<typeof ${_varComponentInstanceA}, typeof ${_varComponentInstanceB}>;\n`);
Expand Down

0 comments on commit 9ae9323

Please sign in to comment.