From ef33bacf2f16ecb4a238e81803b931bf830ea18f Mon Sep 17 00:00:00 2001 From: johnsoncodehk Date: Wed, 31 Aug 2022 01:22:43 +0800 Subject: [PATCH] feat: respect `exactOptionalPropertyTypes` with props fix https://github.com/vuejs/core/issues/6532 --- .../src/generators/script.ts | 32 +++++++++++-------- .../vue-language-core/src/plugins/vue-tsx.ts | 1 + 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/vue-language-core/src/generators/script.ts b/packages/vue-language-core/src/generators/script.ts index 4791cee46..48b0a5447 100644 --- a/packages/vue-language-core/src/generators/script.ts +++ b/packages/vue-language-core/src/generators/script.ts @@ -27,15 +27,16 @@ export function generate( cssModuleClasses: ReturnType, cssScopedClasses: ReturnType, htmlGen: ReturnType | undefined, - compilerOptions: VueCompilerOptions, + compilerOptions: ts.CompilerOptions, + vueCompilerOptions: VueCompilerOptions, codeGen = new CodeGen(), teleports: SourceMaps.Mapping[] = [], ) { - const downgradePropsAndEmitsToSetupReturnOnScriptSetup = compilerOptions.experimentalDowngradePropsAndEmitsToSetupReturnOnScriptSetup === 'onlyJs' + const downgradePropsAndEmitsToSetupReturnOnScriptSetup = vueCompilerOptions.experimentalDowngradePropsAndEmitsToSetupReturnOnScriptSetup === 'onlyJs' ? lang === 'js' || lang === 'jsx' - : !!compilerOptions.experimentalDowngradePropsAndEmitsToSetupReturnOnScriptSetup; - const vueVersion = compilerOptions.target ?? 3; + : !!vueCompilerOptions.experimentalDowngradePropsAndEmitsToSetupReturnOnScriptSetup; + const vueVersion = vueCompilerOptions.target ?? 3; const vueLibName = getVueLibraryName(vueVersion); const usedTypes = { DefinePropsToOptions: false, @@ -131,8 +132,13 @@ export function generate( function writeScriptSetupTypes() { if (usedTypes.DefinePropsToOptions) { - codeGen.addText(`type __VLS_NonUndefinedable = T extends undefined ? never : T;\n`); - codeGen.addText(`type __VLS_TypePropsToRuntimeProps = { [K in keyof T]-?: {} extends Pick ? { type: import('${vueLibName}').PropType<__VLS_NonUndefinedable> } : { type: import('${vueLibName}').PropType, required: true } };\n`); + if (compilerOptions.exactOptionalPropertyTypes) { + codeGen.addText(`type __VLS_TypePropsToRuntimeProps = { [K in keyof T]-?: {} extends Pick ? { type: import('${vueLibName}').PropType } : { type: import('${vueLibName}').PropType, required: true } };\n`); + } + else { + codeGen.addText(`type __VLS_NonUndefinedable = T extends undefined ? never : T;\n`); + codeGen.addText(`type __VLS_TypePropsToRuntimeProps = { [K in keyof T]-?: {} extends Pick ? { type: import('${vueLibName}').PropType<__VLS_NonUndefinedable> } : { type: import('${vueLibName}').PropType, required: true } };\n`); + } } if (usedTypes.mergePropDefaults) { codeGen.addText(`type __VLS_WithDefaults = { @@ -628,7 +634,7 @@ export function generate( /* Components */ codeGen.addText('/* Components */\n'); codeGen.addText('let __VLS_otherComponents!: NonNullable & __VLS_types.GlobalComponents & typeof __VLS_vmUnwrap.components & __VLS_types.PickComponents;\n'); - codeGen.addText(`let __VLS_selfComponent!: __VLS_types.SelfComponent { ${getSlotsPropertyName(compilerOptions.target ?? 3)}: typeof __VLS_slots })>;\n`); + codeGen.addText(`let __VLS_selfComponent!: __VLS_types.SelfComponent { ${getSlotsPropertyName(vueCompilerOptions.target ?? 3)}: typeof __VLS_slots })>;\n`); codeGen.addText('let __VLS_components!: typeof __VLS_otherComponents & Omit;\n'); codeGen.addText(`__VLS_components['${SearchTexts.Components}'];\n`); @@ -785,25 +791,25 @@ export function generate( let shimComponentOptionsMode: 'defineComponent' | 'Vue.extend' | false = false; if ( - compilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend === 'onlyJs' + vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend === 'onlyJs' ? lang === 'js' || lang === 'jsx' - : !!compilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend + : !!vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend ) { shimComponentOptionsMode = 'Vue.extend'; } if ( - compilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent === 'onlyJs' + vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent === 'onlyJs' ? lang === 'js' || lang === 'jsx' - : !!compilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent + : !!vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent ) { shimComponentOptionsMode = 'defineComponent'; } // true override 'onlyJs' - if (compilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend === true) { + if (vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend === true) { shimComponentOptionsMode = 'Vue.extend'; } - if (compilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent === true) { + if (vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent === true) { shimComponentOptionsMode = 'defineComponent'; } diff --git a/packages/vue-language-core/src/plugins/vue-tsx.ts b/packages/vue-language-core/src/plugins/vue-tsx.ts index 6e565643e..c47461d72 100644 --- a/packages/vue-language-core/src/plugins/vue-tsx.ts +++ b/packages/vue-language-core/src/plugins/vue-tsx.ts @@ -148,6 +148,7 @@ const plugin: VueLanguagePlugin = ({ modules, vueCompilerOptions, compilerOption cssModuleClasses.value, cssScopedClasses.value, htmlGen.value, + compilerOptions, vueCompilerOptions, ));