Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): defineProps generates unnecessary array of same types #4353

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }`
Expand Down Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
)
]
Expand All @@ -1716,11 +1716,7 @@ function inferRuntimeType(
}

function toRuntimeTypeString(types: string[]) {
return types.some(t => t === 'null')
? `null`
webfansplz marked this conversation as resolved.
Show resolved Hide resolved
: types.length > 1
? `[${types.join(', ')}]`
: types[0]
return types.length > 1 ? `[${types.join(', ')}]` : types[0]
}

function extractRuntimeEmits(
Expand Down