]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(compiler-sfc): drop Function prop type when no static default value (#7125)
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Mon, 14 Nov 2022 01:15:17 +0000 (09:15 +0800)
committerGitHub <noreply@github.com>
Mon, 14 Nov 2022 01:15:17 +0000 (20:15 -0500)
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index b167dea7026e339fd2ea466c4bb46558a92b2d45..ae42d6f87b9a441c40701924573b54cd04dc079d 100644 (file)
@@ -1784,6 +1784,29 @@ const props = __props as { foo: string, bar?: number, baz: boolean, qux(): numbe
 
       
       
+return { props }
+}
+
+})"
+`;
+
+exports[`SFC compile <script setup> with TypeScript withDefaults (static) w/ production mode 1`] = `
+"import { defineComponent as _defineComponent } from 'vue'
+
+export default /*#__PURE__*/_defineComponent({
+  props: {
+    foo: null,
+    bar: { type: Boolean },
+    baz: { type: [Boolean, Function], default: true },
+    qux: { default: 'hi' }
+  },
+  setup(__props: any, { expose }) {
+  expose();
+
+const props = __props as { foo: () => void, bar: boolean, baz: boolean | (() => void), qux: string | number };
+
+      
+      
 return { props }
 }
 
index b0ab7e2f34e194f0d2a1485606a47add3818ad48..eed363bc031125787e1b6d27e7b8ac5d832103e2 100644 (file)
@@ -1121,6 +1121,36 @@ const emit = defineEmits(['a', 'b'])
       })
     })
 
+    // #7111
+    test('withDefaults (static) w/ production mode', () => {
+      const { content } = compile(
+        `
+      <script setup lang="ts">
+      const props = withDefaults(defineProps<{
+        foo: () => void
+        bar: boolean
+        baz: boolean | (() => void)
+        qux: string | number
+      }>(), {
+        baz: true,
+        qux: 'hi'
+      })
+      </script>
+      `,
+        { isProd: true }
+      )
+      assertCode(content)
+      expect(content).toMatch(`const props = __props`)
+
+      // foo has no default value, the Function can be dropped
+      expect(content).toMatch(`foo: null`)
+      expect(content).toMatch(`bar: { type: Boolean }`)
+      expect(content).toMatch(
+        `baz: { type: [Boolean, Function], default: true }`
+      )
+      expect(content).toMatch(`qux: { default: 'hi' }`)
+    })
+
     test('withDefaults (dynamic)', () => {
       const { content } = compile(`
       <script setup lang="ts">
index 60fafdd44b34b9f89bf923c42ce6d7bf6c8851e1..c87beb1537633838c9b07e66d9aa90281451ebfe 100644 (file)
@@ -822,8 +822,15 @@ export function compileScript(
           )}, required: ${required}${
             defaultString ? `, ${defaultString}` : ``
           } }`
-        } else if (type.some(el => el === 'Boolean' || el === 'Function')) {
-          // #4783, #7111 for boolean or function, should keep the type
+        } else if (
+          type.some(
+            el =>
+              el === 'Boolean' ||
+              ((!hasStaticDefaults || defaultString) && el === 'Function')
+          )
+        ) {
+          // #4783 for boolean, should keep the type
+          // #7111 for function, if default value exists or it's not static, should keep it
           // in production
           return `${key}: { type: ${toRuntimeTypeString(type)}${
             defaultString ? `, ${defaultString}` : ``