]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): add type for props's properties in prod mode (#4790)
authorygj6 <7699524+ygj6@users.noreply.github.com>
Wed, 3 Nov 2021 02:04:04 +0000 (10:04 +0800)
committerGitHub <noreply@github.com>
Wed, 3 Nov 2021 02:04:04 +0000 (22:04 -0400)
fix #4783

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

index a8f95c72a130f343d51ae94be1c7c607543d3ac2..5b11ae5c1a560e4f8cb44d130efa77da68f56875 100644 (file)
@@ -82,7 +82,9 @@ export default /*#__PURE__*/_defineComponent({
   props: {
     foo: { default: 1 },
     bar: { default: () => {} },
-    baz: null
+    baz: null,
+    boola: { type: Boolean },
+    boolb: { type: [Boolean, Number] }
   },
   setup(__props: any) {
 
index f388e501dd9cfa89dc016456366bc158531e2585..5d406608288d92645ad8126d035227604abcf28b 100644 (file)
@@ -83,7 +83,7 @@ describe('sfc props transform', () => {
     const { content } = compile(
       `
       <script setup lang="ts">
-      const { foo = 1, bar = {} } = defineProps<{ foo?: number, bar?: object, baz?: any }>()
+      const { foo = 1, bar = {} } = defineProps<{ foo?: number, bar?: object, baz?: any, boola?: boolean, boolb?: boolean | number }>()
       </script>
     `,
       { isProd: true }
@@ -93,7 +93,9 @@ describe('sfc props transform', () => {
     expect(content).toMatch(`props: {
     foo: { default: 1 },
     bar: { default: () => {} },
-    baz: null
+    baz: null,
+    boola: { type: Boolean },
+    boolb: { type: [Boolean, Number] }
   }`)
     assertCode(content)
   })
index 0e60d0d9e312563418f2e48954fe17bc825510cc..dcaf3848caab0610cf98c74adc3d7f5c77e1541b 100644 (file)
@@ -685,13 +685,20 @@ export function compileScript(
           }
         }
 
+        const { type, required } = props[key]
         if (!isProd) {
-          const { type, required } = props[key]
           return `${key}: { type: ${toRuntimeTypeString(
             type
           )}, required: ${required}${
             defaultString ? `, ${defaultString}` : ``
           } }`
+        } else if (type.indexOf('Boolean') > -1) {
+          // production: if boolean exists, should keep the type.
+          return `${key}: { type: ${toRuntimeTypeString(
+            type
+          )}${
+            defaultString ? `, ${defaultString}` : ``
+          } }`
         } else {
           // production: checks are useless
           return `${key}: ${defaultString ? `{ ${defaultString} }` : 'null'}`
@@ -1621,15 +1628,13 @@ function extractRuntimeProps(
       m.key.type === 'Identifier'
     ) {
       let type
-      if (!isProd) {
-        if (m.type === 'TSMethodSignature') {
-          type = ['Function']
-        } else if (m.typeAnnotation) {
-          type = inferRuntimeType(
-            m.typeAnnotation.typeAnnotation,
-            declaredTypes
-          )
-        }
+      if (m.type === 'TSMethodSignature') {
+        type = ['Function']
+      } else if (m.typeAnnotation) {
+        type = inferRuntimeType(
+          m.typeAnnotation.typeAnnotation,
+          declaredTypes
+        )
       }
       props[m.key.name] = {
         key: m.key.name,