Skip to content

Commit a7bcc1a

Browse files
authored
fix(language-core): add undefined to first param type of optional model emits (#5171)
1 parent 4584faf commit a7bcc1a

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

Diff for: packages/language-core/lib/codegen/script/scriptSetup.ts

+3
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,9 @@ function* generateModelEmit(
524524
const [propName, localName] = getPropAndLocalName(scriptSetup, defineModel);
525525
yield `'update:${propName}': [value: `;
526526
yield* generateDefinePropType(scriptSetup, propName, localName, defineModel);
527+
if (!defineModel.required) {
528+
yield ` | undefined`;
529+
}
527530
yield `]${endOfLine}`;
528531
}
529532
yield `}${endOfLine}`;

Diff for: test-workspace/tsc/passedFixtures/vue3.4/defineModel/main.vue

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ const ScriptSetupExact = defineComponent({
1313
g: PropType<string>;
1414
},
1515
emits: {} as {
16-
"update:modelValue": (_:string) => void
17-
"update:c": (_:number) => void
18-
"update:d": (_:number) => void
19-
"update:e": (_:string) => void
20-
"update:f": (_:string) => void
21-
"update:g": (_:string) => void
16+
"update:modelValue": (_: string | undefined) => void
17+
"update:c": (_: number) => void
18+
"update:d": (_: number | undefined) => void
19+
"update:e": (_: string | undefined) => void
20+
"update:f": (_: string) => void
21+
"update:g": (_: string | undefined) => void
2222
},
2323
setup() {
2424
return {};
@@ -32,11 +32,11 @@ exactType(ScriptSetup, ScriptSetupExact);
3232
<ScriptSetup
3333
:c="0"
3434
f=""
35-
@update:model-value="(x) => exactType(x, {} as string)"
35+
@update:model-value="(x) => exactType(x, {} as string | undefined)"
3636
@update:c="(x) => exactType(x, {} as number)"
37-
@update:d="(x) => exactType(x, {} as number)"
38-
@update:e="(x) => exactType(x, {} as string)"
37+
@update:d="(x) => exactType(x, {} as number | undefined)"
38+
@update:e="(x) => exactType(x, {} as string | undefined)"
3939
@update:f="(x) => exactType(x, {} as string)"
40-
@update:g="(x) => exactType(x, {} as string)"
40+
@update:g="(x) => exactType(x, {} as string | undefined)"
4141
/>
4242
</template>

Diff for: test-workspace/tsc/passedFixtures/vue3/defineModel/main.vue

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ const ScriptSetupExact = defineComponent({
1313
'g'?: string,
1414
},
1515
__typeEmits: {} as {
16-
'update:modelValue': [modelValue:string];
17-
'update:c': [c:number];
18-
'update:d': [d:number];
19-
'update:e': [e:string];
20-
'update:f': [f:string];
21-
'update:g': [g:string];
16+
'update:modelValue': [modelValue: string | undefined];
17+
'update:c': [c: number];
18+
'update:d': [d: number | undefined];
19+
'update:e': [e: string];
20+
'update:f': [f: string | undefined];
21+
'update:g': [g: string | undefined];
2222
},
2323
setup() {
2424
return {};
@@ -32,11 +32,11 @@ exactType(ScriptSetup, ScriptSetupExact);
3232
<ScriptSetup
3333
:c="0"
3434
e=""
35-
@update:model-value="(x) => exactType(x, {} as string)"
35+
@update:model-value="(x) => exactType(x, {} as string | undefined)"
3636
@update:c="(x) => exactType(x, {} as number)"
37-
@update:d="(x) => exactType(x, {} as number)"
37+
@update:d="(x) => exactType(x, {} as number | undefined)"
3838
@update:e="(x) => exactType(x, {} as string)"
39-
@update:f="(x) => exactType(x, {} as string)"
40-
@update:g="(x) => exactType(x, {} as string)"
39+
@update:f="(x) => exactType(x, {} as string | undefined)"
40+
@update:g="(x) => exactType(x, {} as string | undefined)"
4141
/>
4242
</template>

0 commit comments

Comments
 (0)