Skip to content

Commit

Permalink
fix(compiler-sfc): defineProps infer TSParenthesizedType (#4147)
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 authored Jul 19, 2021
1 parent 47ba33e commit f7607d3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,8 @@ export default _defineComponent({
union: { type: [String, Number], required: true },
literalUnion: { type: [String, String], required: true },
literalUnionMixed: { type: [String, Number, Boolean], required: true },
intersection: { type: Object, required: true }
intersection: { type: Object, required: true },
foo: { type: [Function, null], required: true }
} as unknown as undefined,
setup(__props: {
string: string
Expand All @@ -787,6 +788,7 @@ export default _defineComponent({
literalUnion: 'foo' | 'bar'
literalUnionMixed: 'foo' | 1 | boolean
intersection: Test & {}
foo: ((item: any) => boolean) | null
}, { expose }) {
expose()
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 @@ -514,6 +514,7 @@ const emit = defineEmits(['a', 'b'])
literalUnion: 'foo' | 'bar'
literalUnionMixed: 'foo' | 1 | boolean
intersection: Test & {}
foo: ((item: any) => boolean) | null
}>()
</script>`)
assertCode(content)
Expand Down Expand Up @@ -545,6 +546,7 @@ const emit = defineEmits(['a', 'b'])
`literalUnionMixed: { type: [String, Number, Boolean], required: true }`
)
expect(content).toMatch(`intersection: { type: Object, required: true }`)
expect(content).toMatch(`foo: { type: [Function, null], required: true }`)
expect(bindings).toStrictEqual({
string: BindingTypes.PROPS,
number: BindingTypes.PROPS,
Expand All @@ -567,7 +569,8 @@ const emit = defineEmits(['a', 'b'])
union: BindingTypes.PROPS,
literalUnion: BindingTypes.PROPS,
literalUnionMixed: BindingTypes.PROPS,
intersection: BindingTypes.PROPS
intersection: BindingTypes.PROPS,
foo: BindingTypes.PROPS
})
})

Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,8 @@ function inferRuntimeType(
}
return [`null`]

case 'TSParenthesizedType':
return inferRuntimeType(node.typeAnnotation, declaredTypes)
case 'TSUnionType':
return [
...new Set(
Expand All @@ -1654,7 +1656,6 @@ function inferRuntimeType(
) as any)
)
]

case 'TSIntersectionType':
return ['Object']

Expand Down

0 comments on commit f7607d3

Please sign in to comment.