Skip to content

Commit 0cef65c

Browse files
fix(compiler-sfc): fix defineModel coercion for boolean + string union types (#9603)
close #9587 close #10676
1 parent 67722ba commit 0cef65c

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

Diff for: packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineModel.spec.ts.snap

+20
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ return { modelValue }
103103
})"
104104
`;
105105

106+
exports[`defineModel() > w/ Boolean And Function types, production mode 1`] = `
107+
"import { useModel as _useModel, defineComponent as _defineComponent } from 'vue'
108+
109+
export default /*#__PURE__*/_defineComponent({
110+
props: {
111+
"modelValue": { type: [Boolean, String] },
112+
"modelModifiers": {},
113+
},
114+
emits: ["update:modelValue"],
115+
setup(__props, { expose: __expose }) {
116+
__expose();
117+
118+
const modelValue = _useModel<boolean | string>(__props, "modelValue")
119+
120+
return { modelValue }
121+
}
122+
123+
})"
124+
`;
125+
106126
exports[`defineModel() > w/ array props 1`] = `
107127
"import { useModel as _useModel, mergeModels as _mergeModels } from 'vue'
108128

Diff for: packages/compiler-sfc/__tests__/compileScript/defineModel.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,24 @@ describe('defineModel()', () => {
221221
assertCode(content)
222222
expect(content).toMatch(`set: (v) => { return v + __props.x }`)
223223
})
224+
225+
test('w/ Boolean And Function types, production mode', () => {
226+
const { content, bindings } = compile(
227+
`
228+
<script setup lang="ts">
229+
const modelValue = defineModel<boolean | string>()
230+
</script>
231+
`,
232+
{ isProd: true },
233+
)
234+
assertCode(content)
235+
expect(content).toMatch('"modelValue": { type: [Boolean, String] }')
236+
expect(content).toMatch('emits: ["update:modelValue"]')
237+
expect(content).toMatch(
238+
`const modelValue = _useModel<boolean | string>(__props, "modelValue")`,
239+
)
240+
expect(bindings).toStrictEqual({
241+
modelValue: BindingTypes.SETUP_REF,
242+
})
243+
})
224244
})

Diff for: packages/compiler-sfc/src/script/defineModel.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,19 @@ export function genModelProps(ctx: ScriptCompileContext) {
129129

130130
let runtimeTypes = type && inferRuntimeType(ctx, type)
131131
if (runtimeTypes) {
132+
const hasBoolean = runtimeTypes.includes('Boolean')
132133
const hasUnknownType = runtimeTypes.includes(UNKNOWN_TYPE)
133134

134-
runtimeTypes = runtimeTypes.filter(el => {
135-
if (el === UNKNOWN_TYPE) return false
136-
return isProd
137-
? el === 'Boolean' || (el === 'Function' && options)
138-
: true
139-
})
140-
skipCheck = !isProd && hasUnknownType && runtimeTypes.length > 0
135+
if (isProd || hasUnknownType) {
136+
runtimeTypes = runtimeTypes.filter(
137+
t =>
138+
t === 'Boolean' ||
139+
(hasBoolean && t === 'String') ||
140+
(t === 'Function' && options),
141+
)
142+
143+
skipCheck = !isProd && hasUnknownType && runtimeTypes.length > 0
144+
}
141145
}
142146

143147
let runtimeType =

0 commit comments

Comments
 (0)