Skip to content

Commit

Permalink
feat: avoid props type integrate by tsc
Browse files Browse the repository at this point in the history
close #799
  • Loading branch information
johnsoncodehk committed Jan 13, 2022
1 parent 5c657c9 commit b04673a
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions packages/vue-code-gen/src/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ export function generate(
codeGen.addText(`type __VLS_DefinePropsToOptions<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: import('${vueLibName}').PropType<__VLS_NonUndefinedable<T[K]>> } : { type: import('${vueLibName}').PropType<T[K]>, required: true } };\n`);
}
if (usedTypes.mergePropDefaults) {
codeGen.addText(`declare function __VLS_mergePropDefaults<P, D>(props: P, defaults: D): {
codeGen.addText(`type __VLS_MargePropDefaults<P, D> = {
[K in keyof P]: K extends keyof D ? P[K] & {
default: D[K]
} : P[K]
}\n`);
};\n`);
}
if (usedTypes.ConstructorOverloads) {
if (scriptSetupRanges && scriptSetupRanges.emitsTypeNums !== -1) {
Expand Down Expand Up @@ -208,6 +208,12 @@ export function generate(
}
function writeExportComponent() {

if (scriptSetupRanges?.withDefaultsArg) {
codeGen.addText(`const __VLS_withDefaultsArg = (`);
mapSubText('scriptSetup', scriptSetupRanges.withDefaultsArg.start, scriptSetupRanges.withDefaultsArg.end);
codeGen.addText(`);\n`);
}

codeGen.addText(`export default (await import('${vueLibName}')).defineComponent({\n`);

if (script && scriptRanges?.exportDefault?.args) {
Expand All @@ -221,21 +227,20 @@ export function generate(
codeGen.addText(`props: (`);
if (scriptSetupRanges.withDefaultsArg) {
usedTypes.mergePropDefaults = true;
codeGen.addText(`__VLS_mergePropDefaults(`);
codeGen.addText(`{} as __VLS_MargePropDefaults<`);
}
if (scriptSetupRanges.propsRuntimeArg) {
mapSubText('scriptSetup', scriptSetupRanges.propsRuntimeArg.start, scriptSetupRanges.propsRuntimeArg.end);
}
else if (scriptSetupRanges.propsTypeArg) {
usedTypes.DefinePropsToOptions = true;
codeGen.addText(`{} as __VLS_DefinePropsToOptions<`);
codeGen.addText(`__VLS_DefinePropsToOptions<`);
mapSubText('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end);
codeGen.addText(`>`);
}
if (scriptSetupRanges.withDefaultsArg) {
codeGen.addText(`, `);
mapSubText('scriptSetup', scriptSetupRanges.withDefaultsArg.start, scriptSetupRanges.withDefaultsArg.end);
codeGen.addText(`)`);
codeGen.addText(`, typeof __VLS_withDefaultsArg`);
codeGen.addText(`>`);
}
codeGen.addText(`),\n`);
}
Expand Down

0 comments on commit b04673a

Please sign in to comment.