]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(defineProps): defineProps generates unnecessary array of same types (#4353)
authorwebfansplz <308241863@qq.com>
Mon, 16 Aug 2021 21:40:00 +0000 (05:40 +0800)
committerGitHub <noreply@github.com>
Mon, 16 Aug 2021 21:40:00 +0000 (17:40 -0400)
fix #4352

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

index 6c76e5fe75b05abd98a6e42a7d0074ddd31cd59c..0974a1d59a338fd56338d2af3b92b8336ad0941d 100644 (file)
@@ -790,7 +790,8 @@ export default _defineComponent({
     alias: { type: Array, required: true },
     method: { type: Function, required: true },
     union: { type: [String, Number], required: true },
-    literalUnion: { type: [String, String], required: true },
+    literalUnion: { type: String, required: true },
+    literalUnionNumber: { type: Number, required: true },
     literalUnionMixed: { type: [String, Number, Boolean], required: true },
     intersection: { type: Object, required: true },
     foo: { type: [Function, null], required: true }
@@ -817,6 +818,7 @@ export default _defineComponent({
 
         union: string | number
         literalUnion: 'foo' | 'bar'
+        literalUnionNumber: 1 | 2 | 3 | 4 | 5
         literalUnionMixed: 'foo' | 1 | boolean
         intersection: Test & {}
         foo: ((item: any) => boolean) | null
index 406462f399701eb1c6ce73c7e0d1d91f7eaef71a..e5be15b076a1b47c3d208f2e156bb1f37a05a3eb 100644 (file)
@@ -538,6 +538,7 @@ const emit = defineEmits(['a', 'b'])
 
         union: string | number
         literalUnion: 'foo' | 'bar'
+        literalUnionNumber: 1 | 2 | 3 | 4 | 5
         literalUnionMixed: 'foo' | 1 | boolean
         intersection: Test & {}
         foo: ((item: any) => boolean) | null
@@ -565,8 +566,9 @@ const emit = defineEmits(['a', 'b'])
       expect(content).toMatch(
         `union: { type: [String, Number], required: true }`
       )
+      expect(content).toMatch(`literalUnion: { type: String, required: true }`)
       expect(content).toMatch(
-        `literalUnion: { type: [String, String], required: true }`
+        `literalUnionNumber: { type: Number, required: true }`
       )
       expect(content).toMatch(
         `literalUnionMixed: { type: [String, Number, Boolean], required: true }`
@@ -594,6 +596,7 @@ const emit = defineEmits(['a', 'b'])
         method: BindingTypes.PROPS,
         union: BindingTypes.PROPS,
         literalUnion: BindingTypes.PROPS,
+        literalUnionNumber: BindingTypes.PROPS,
         literalUnionMixed: BindingTypes.PROPS,
         intersection: BindingTypes.PROPS,
         foo: BindingTypes.PROPS
index 914cf28353110607b348e9e641bcd082a7f68516..8a6b9e60f7ffca6d4af89dfdf9d5ca611ad0c9dd 100644 (file)
@@ -1703,7 +1703,7 @@ function inferRuntimeType(
       return [
         ...new Set(
           [].concat(
-            node.types.map(t => inferRuntimeType(t, declaredTypes)) as any
+            ...(node.types.map(t => inferRuntimeType(t, declaredTypes)) as any)
           )
         )
       ]
@@ -1716,11 +1716,7 @@ function inferRuntimeType(
 }
 
 function toRuntimeTypeString(types: string[]) {
-  return types.some(t => t === 'null')
-    ? `null`
-    : types.length > 1
-    ? `[${types.join(', ')}]`
-    : types[0]
+  return types.length > 1 ? `[${types.join(', ')}]` : types[0]
 }
 
 function extractRuntimeEmits(