Skip to content

Commit a82a56e

Browse files
author
RicardoErii
committed
fix(compiler-sfc): fixed the type compilation of defineModel in the prod
1 parent f18a174 commit a82a56e

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ return { modelValue, c, toString }
2323
}"
2424
`;
2525

26+
exports[`defineModel() > w/ Boolean And Function types, production mode 1`] = `
27+
"import { useModel as _useModel, defineComponent as _defineComponent } from 'vue'
28+
29+
export default /*#__PURE__*/_defineComponent({
30+
props: {
31+
\\"modelValue\\": { type: [Boolean, String] },
32+
\\"fn\\": {},
33+
\\"fnWithDefault\\": { type: Function, ...{ default: () => null } },
34+
\\"optional\\": { required: false },
35+
},
36+
emits: [\\"update:modelValue\\", \\"update:fn\\", \\"update:fnWithDefault\\", \\"update:optional\\"],
37+
setup(__props, { expose: __expose }) {
38+
__expose();
39+
40+
const modelValue = _useModel(__props, \\"modelValue\\")
41+
const fn = _useModel(__props, \\"fn\\")
42+
const fnWithDefault = _useModel(__props, \\"fnWithDefault\\")
43+
const optional = _useModel(__props, \\"optional\\")
44+
45+
return { modelValue, fn, fnWithDefault, optional }
46+
}
47+
48+
})"
49+
`;
50+
2651
exports[`defineModel() > w/ array props 1`] = `
2752
"import { useModel as _useModel, mergeModels as _mergeModels } from 'vue'
2853

packages/compiler-sfc/__tests__/compileScript/defineModel.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,38 @@ describe('defineModel()', () => {
181181
optional: BindingTypes.SETUP_REF
182182
})
183183
})
184+
185+
test('w/ Boolean And Function types, production mode', () => {
186+
const { content, bindings } = compile(
187+
`
188+
<script setup lang="ts">
189+
const modelValue = defineModel<boolean | string>()
190+
const fn = defineModel<() => void>('fn')
191+
const fnWithDefault = defineModel<() => void>('fnWithDefault', { default: () => null })
192+
const optional = defineModel<string>('optional', { required: false })
193+
</script>
194+
`,
195+
{ defineModel: true, isProd: true }
196+
)
197+
assertCode(content)
198+
expect(content).toMatch('"modelValue": { type: [Boolean, String] }')
199+
expect(content).toMatch('"fn": {}')
200+
expect(content).toMatch(
201+
'"fnWithDefault": { type: Function, ...{ default: () => null } },'
202+
)
203+
expect(content).toMatch('"optional": { required: false }')
204+
expect(content).toMatch(
205+
'emits: ["update:modelValue", "update:fn", "update:fnWithDefault", "update:optional"]'
206+
)
207+
expect(content).toMatch(
208+
`const modelValue = _useModel(__props, "modelValue")`
209+
)
210+
expect(content).toMatch(`const fn = _useModel(__props, "fn")`)
211+
expect(bindings).toStrictEqual({
212+
modelValue: BindingTypes.SETUP_REF,
213+
fn: BindingTypes.SETUP_REF,
214+
fnWithDefault: BindingTypes.SETUP_REF,
215+
optional: BindingTypes.SETUP_REF
216+
})
217+
})
184218
})

packages/compiler-sfc/src/script/defineModel.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,16 @@ export function genModelProps(ctx: ScriptCompileContext) {
120120
if (runtimeTypes) {
121121
const hasUnknownType = runtimeTypes.includes(UNKNOWN_TYPE)
122122

123-
runtimeTypes = runtimeTypes.filter(el => {
124-
if (el === UNKNOWN_TYPE) return false
125-
return isProd
126-
? el === 'Boolean' || (el === 'Function' && options)
127-
: true
128-
})
123+
runtimeTypes = runtimeTypes.filter(el => el !== UNKNOWN_TYPE)
124+
125+
if (
126+
isProd &&
127+
!runtimeTypes.some(
128+
el => el === 'Boolean' || (el === 'Function' && options)
129+
)
130+
) {
131+
runtimeTypes = []
132+
}
129133
skipCheck = !isProd && hasUnknownType && runtimeTypes.length > 0
130134
}
131135

0 commit comments

Comments
 (0)