]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): always generate runtime prop type for Function (#7112)
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Mon, 14 Nov 2022 00:36:03 +0000 (08:36 +0800)
committerGitHub <noreply@github.com>
Mon, 14 Nov 2022 00:36:03 +0000 (19:36 -0500)
fix #7111

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

index 80fb1f087cc058a06a0dec67a3a855a13f0fd87d..b167dea7026e339fd2ea466c4bb46558a92b2d45 100644 (file)
@@ -1729,6 +1729,35 @@ const props = __props as {
 
       
       
+return { props, get defaults() { return defaults } }
+}
+
+})"
+`;
+
+exports[`SFC compile <script setup> with TypeScript withDefaults (dynamic) w/ production mode 1`] = `
+"import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue'
+import { defaults } from './foo'
+      
+export default /*#__PURE__*/_defineComponent({
+  props: _mergeDefaults({
+    foo: { type: Function },
+    bar: { type: Boolean },
+    baz: { type: [Boolean, Function] },
+    qux: null
+  }, { ...defaults }),
+  setup(__props: any, { expose }) {
+  expose();
+
+const props = __props as {
+        foo: () => void
+        bar: boolean
+        baz: boolean | (() => void)
+        qux: string | number
+      };
+
+      
+      
 return { props, get defaults() { return defaults } }
 }
 
index 4b73b9e93a60f36eeecf8dc07f3460e2c3982805..b0ab7e2f34e194f0d2a1485606a47add3818ad48 100644 (file)
@@ -1144,6 +1144,35 @@ const emit = defineEmits(['a', 'b'])
       )
     })
 
+    // #7111
+    test('withDefaults (dynamic) w/ production mode', () => {
+      const { content } = compile(
+        `
+      <script setup lang="ts">
+      import { defaults } from './foo'
+      const props = withDefaults(defineProps<{
+        foo: () => void
+        bar: boolean
+        baz: boolean | (() => void)
+        qux: string | number
+      }>(), { ...defaults })
+      </script>
+      `,
+        { isProd: true }
+      )
+      assertCode(content)
+      expect(content).toMatch(`import { mergeDefaults as _mergeDefaults`)
+      expect(content).toMatch(
+        `
+  _mergeDefaults({
+    foo: { type: Function },
+    bar: { type: Boolean },
+    baz: { type: [Boolean, Function] },
+    qux: null
+  }, { ...defaults })`.trim()
+      )
+    })
+
     test('defineEmits w/ type', () => {
       const { content } = compile(`
       <script setup lang="ts">
index 7f6b087a268a0b23b5e2a48e66c6604fc69983c4..60fafdd44b34b9f89bf923c42ce6d7bf6c8851e1 100644 (file)
@@ -822,12 +822,9 @@ export function compileScript(
           )}, required: ${required}${
             defaultString ? `, ${defaultString}` : ``
           } }`
-        } else if (
-          type.some(
-            el => el === 'Boolean' || (defaultString && el === 'Function')
-          )
-        ) {
-          // #4783 production: if boolean or defaultString and function exists, should keep the type.
+        } else if (type.some(el => el === 'Boolean' || el === 'Function')) {
+          // #4783, #7111 for boolean or function, should keep the type
+          // in production
           return `${key}: { type: ${toRuntimeTypeString(type)}${
             defaultString ? `, ${defaultString}` : ``
           } }`