Skip to content

Commit ef33bac

Browse files
committed
feat: respect exactOptionalPropertyTypes with props
fix vuejs/core#6532
1 parent 94f6d5b commit ef33bac

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

packages/vue-language-core/src/generators/script.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ export function generate(
2727
cssModuleClasses: ReturnType<typeof collectStyleCssClasses>,
2828
cssScopedClasses: ReturnType<typeof collectStyleCssClasses>,
2929
htmlGen: ReturnType<typeof templateGen['generate']> | undefined,
30-
compilerOptions: VueCompilerOptions,
30+
compilerOptions: ts.CompilerOptions,
31+
vueCompilerOptions: VueCompilerOptions,
3132
codeGen = new CodeGen<EmbeddedFileMappingData>(),
3233
teleports: SourceMaps.Mapping<TeleportMappingData>[] = [],
3334
) {
3435

35-
const downgradePropsAndEmitsToSetupReturnOnScriptSetup = compilerOptions.experimentalDowngradePropsAndEmitsToSetupReturnOnScriptSetup === 'onlyJs'
36+
const downgradePropsAndEmitsToSetupReturnOnScriptSetup = vueCompilerOptions.experimentalDowngradePropsAndEmitsToSetupReturnOnScriptSetup === 'onlyJs'
3637
? lang === 'js' || lang === 'jsx'
37-
: !!compilerOptions.experimentalDowngradePropsAndEmitsToSetupReturnOnScriptSetup;
38-
const vueVersion = compilerOptions.target ?? 3;
38+
: !!vueCompilerOptions.experimentalDowngradePropsAndEmitsToSetupReturnOnScriptSetup;
39+
const vueVersion = vueCompilerOptions.target ?? 3;
3940
const vueLibName = getVueLibraryName(vueVersion);
4041
const usedTypes = {
4142
DefinePropsToOptions: false,
@@ -131,8 +132,13 @@ export function generate(
131132

132133
function writeScriptSetupTypes() {
133134
if (usedTypes.DefinePropsToOptions) {
134-
codeGen.addText(`type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;\n`);
135-
codeGen.addText(`type __VLS_TypePropsToRuntimeProps<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`);
135+
if (compilerOptions.exactOptionalPropertyTypes) {
136+
codeGen.addText(`type __VLS_TypePropsToRuntimeProps<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? { type: import('${vueLibName}').PropType<T[K]> } : { type: import('${vueLibName}').PropType<T[K]>, required: true } };\n`);
137+
}
138+
else {
139+
codeGen.addText(`type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;\n`);
140+
codeGen.addText(`type __VLS_TypePropsToRuntimeProps<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`);
141+
}
136142
}
137143
if (usedTypes.mergePropDefaults) {
138144
codeGen.addText(`type __VLS_WithDefaults<P, D> = {
@@ -628,7 +634,7 @@ export function generate(
628634
/* Components */
629635
codeGen.addText('/* Components */\n');
630636
codeGen.addText('let __VLS_otherComponents!: NonNullable<typeof __VLS_component extends { components: infer C } ? C : {}> & __VLS_types.GlobalComponents & typeof __VLS_vmUnwrap.components & __VLS_types.PickComponents<typeof __VLS_ctx>;\n');
631-
codeGen.addText(`let __VLS_selfComponent!: __VLS_types.SelfComponent<typeof __VLS_name, typeof __VLS_component & (new () => { ${getSlotsPropertyName(compilerOptions.target ?? 3)}: typeof __VLS_slots })>;\n`);
637+
codeGen.addText(`let __VLS_selfComponent!: __VLS_types.SelfComponent<typeof __VLS_name, typeof __VLS_component & (new () => { ${getSlotsPropertyName(vueCompilerOptions.target ?? 3)}: typeof __VLS_slots })>;\n`);
632638
codeGen.addText('let __VLS_components!: typeof __VLS_otherComponents & Omit<typeof __VLS_selfComponent, keyof typeof __VLS_otherComponents>;\n');
633639

634640
codeGen.addText(`__VLS_components['${SearchTexts.Components}'];\n`);
@@ -785,25 +791,25 @@ export function generate(
785791
let shimComponentOptionsMode: 'defineComponent' | 'Vue.extend' | false = false;
786792

787793
if (
788-
compilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend === 'onlyJs'
794+
vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend === 'onlyJs'
789795
? lang === 'js' || lang === 'jsx'
790-
: !!compilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend
796+
: !!vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend
791797
) {
792798
shimComponentOptionsMode = 'Vue.extend';
793799
}
794800
if (
795-
compilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent === 'onlyJs'
801+
vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent === 'onlyJs'
796802
? lang === 'js' || lang === 'jsx'
797-
: !!compilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent
803+
: !!vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent
798804
) {
799805
shimComponentOptionsMode = 'defineComponent';
800806
}
801807

802808
// true override 'onlyJs'
803-
if (compilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend === true) {
809+
if (vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithVue2Extend === true) {
804810
shimComponentOptionsMode = 'Vue.extend';
805811
}
806-
if (compilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent === true) {
812+
if (vueCompilerOptions.experimentalImplicitWrapComponentOptionsWithDefineComponent === true) {
807813
shimComponentOptionsMode = 'defineComponent';
808814
}
809815

packages/vue-language-core/src/plugins/vue-tsx.ts

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ const plugin: VueLanguagePlugin = ({ modules, vueCompilerOptions, compilerOption
148148
cssModuleClasses.value,
149149
cssScopedClasses.value,
150150
htmlGen.value,
151+
compilerOptions,
151152
vueCompilerOptions,
152153
));
153154

0 commit comments

Comments
 (0)