Skip to content

Commit ad66295

Browse files
authored
fix(defineProps): defineProps generates unnecessary array of same types (#4353)
fix #4352
1 parent 77223df commit ad66295

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ export default _defineComponent({
790790
alias: { type: Array, required: true },
791791
method: { type: Function, required: true },
792792
union: { type: [String, Number], required: true },
793-
literalUnion: { type: [String, String], required: true },
793+
literalUnion: { type: String, required: true },
794+
literalUnionNumber: { type: Number, required: true },
794795
literalUnionMixed: { type: [String, Number, Boolean], required: true },
795796
intersection: { type: Object, required: true },
796797
foo: { type: [Function, null], required: true }
@@ -817,6 +818,7 @@ export default _defineComponent({
817818
818819
union: string | number
819820
literalUnion: 'foo' | 'bar'
821+
literalUnionNumber: 1 | 2 | 3 | 4 | 5
820822
literalUnionMixed: 'foo' | 1 | boolean
821823
intersection: Test & {}
822824
foo: ((item: any) => boolean) | null

packages/compiler-sfc/__tests__/compileScript.spec.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ const emit = defineEmits(['a', 'b'])
538538
539539
union: string | number
540540
literalUnion: 'foo' | 'bar'
541+
literalUnionNumber: 1 | 2 | 3 | 4 | 5
541542
literalUnionMixed: 'foo' | 1 | boolean
542543
intersection: Test & {}
543544
foo: ((item: any) => boolean) | null
@@ -565,8 +566,9 @@ const emit = defineEmits(['a', 'b'])
565566
expect(content).toMatch(
566567
`union: { type: [String, Number], required: true }`
567568
)
569+
expect(content).toMatch(`literalUnion: { type: String, required: true }`)
568570
expect(content).toMatch(
569-
`literalUnion: { type: [String, String], required: true }`
571+
`literalUnionNumber: { type: Number, required: true }`
570572
)
571573
expect(content).toMatch(
572574
`literalUnionMixed: { type: [String, Number, Boolean], required: true }`
@@ -594,6 +596,7 @@ const emit = defineEmits(['a', 'b'])
594596
method: BindingTypes.PROPS,
595597
union: BindingTypes.PROPS,
596598
literalUnion: BindingTypes.PROPS,
599+
literalUnionNumber: BindingTypes.PROPS,
597600
literalUnionMixed: BindingTypes.PROPS,
598601
intersection: BindingTypes.PROPS,
599602
foo: BindingTypes.PROPS

packages/compiler-sfc/src/compileScript.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ function inferRuntimeType(
17031703
return [
17041704
...new Set(
17051705
[].concat(
1706-
node.types.map(t => inferRuntimeType(t, declaredTypes)) as any
1706+
...(node.types.map(t => inferRuntimeType(t, declaredTypes)) as any)
17071707
)
17081708
)
17091709
]
@@ -1716,11 +1716,7 @@ function inferRuntimeType(
17161716
}
17171717

17181718
function toRuntimeTypeString(types: string[]) {
1719-
return types.some(t => t === 'null')
1720-
? `null`
1721-
: types.length > 1
1722-
? `[${types.join(', ')}]`
1723-
: types[0]
1719+
return types.length > 1 ? `[${types.join(', ')}]` : types[0]
17241720
}
17251721

17261722
function extractRuntimeEmits(

0 commit comments

Comments
 (0)