]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): fix defineModel coercion for boolean + string union types (#9603)
authoryangxiuxiu <79584569+yangxiuxiu1115@users.noreply.github.com>
Mon, 15 Apr 2024 13:18:59 +0000 (21:18 +0800)
committerGitHub <noreply@github.com>
Mon, 15 Apr 2024 13:18:59 +0000 (21:18 +0800)
close #9587
close #10676

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

index b6a93541d36d4d300c99799cc60d1addb7e592d4..6e9967fd0116c88002ab54fdcf356a9ec646186d 100644 (file)
@@ -103,6 +103,26 @@ return { modelValue }
 })"
 `;
 
+exports[`defineModel() > w/ Boolean And Function types, production mode 1`] = `
+"import { useModel as _useModel, defineComponent as _defineComponent } from 'vue'
+
+export default /*#__PURE__*/_defineComponent({
+  props: {
+    "modelValue": { type: [Boolean, String] },
+    "modelModifiers": {},
+  },
+  emits: ["update:modelValue"],
+  setup(__props, { expose: __expose }) {
+  __expose();
+
+      const modelValue = _useModel<boolean | string>(__props, "modelValue")
+      
+return { modelValue }
+}
+
+})"
+`;
+
 exports[`defineModel() > w/ array props 1`] = `
 "import { useModel as _useModel, mergeModels as _mergeModels } from 'vue'
 
index 304258615a85fbf7864a437e36ee1659cf3dd219..bd048a847e487c5de10f157e99b662fb2e30cb67 100644 (file)
@@ -221,4 +221,24 @@ describe('defineModel()', () => {
     assertCode(content)
     expect(content).toMatch(`set: (v) => { return v + __props.x }`)
   })
+
+  test('w/ Boolean And Function types, production mode', () => {
+    const { content, bindings } = compile(
+      `
+      <script setup lang="ts">
+      const modelValue = defineModel<boolean | string>()
+      </script>
+      `,
+      { isProd: true },
+    )
+    assertCode(content)
+    expect(content).toMatch('"modelValue": { type: [Boolean, String] }')
+    expect(content).toMatch('emits: ["update:modelValue"]')
+    expect(content).toMatch(
+      `const modelValue = _useModel<boolean | string>(__props, "modelValue")`,
+    )
+    expect(bindings).toStrictEqual({
+      modelValue: BindingTypes.SETUP_REF,
+    })
+  })
 })
index 24fd0780eaa6cea85f0fcc21a95558db56322131..e5e2ed0e53fc3faae22ed27674852c1d65bf4b8b 100644 (file)
@@ -129,15 +129,19 @@ export function genModelProps(ctx: ScriptCompileContext) {
 
     let runtimeTypes = type && inferRuntimeType(ctx, type)
     if (runtimeTypes) {
+      const hasBoolean = runtimeTypes.includes('Boolean')
       const hasUnknownType = runtimeTypes.includes(UNKNOWN_TYPE)
 
-      runtimeTypes = runtimeTypes.filter(el => {
-        if (el === UNKNOWN_TYPE) return false
-        return isProd
-          ? el === 'Boolean' || (el === 'Function' && options)
-          : true
-      })
-      skipCheck = !isProd && hasUnknownType && runtimeTypes.length > 0
+      if (isProd || hasUnknownType) {
+        runtimeTypes = runtimeTypes.filter(
+          t =>
+            t === 'Boolean' ||
+            (hasBoolean && t === 'String') ||
+            (t === 'Function' && options),
+        )
+
+        skipCheck = !isProd && hasUnknownType && runtimeTypes.length > 0
+      }
     }
 
     let runtimeType =