fix(compiler-sfc): support method signature in defineProps

fix #2983
This commit is contained in:
Evan You 2021-06-28 15:39:04 -04:00
parent 2f91db30cd
commit afdd2f2835
3 changed files with 21 additions and 5 deletions

View File

@ -864,6 +864,7 @@ export default _defineComponent({
recordRef: { type: Object, required: true }, recordRef: { type: Object, required: true },
interface: { type: Object, required: true }, interface: { type: Object, required: true },
alias: { type: Array, required: true }, alias: { type: Array, required: true },
method: { type: Function, required: true },
union: { type: [String, Number], required: true }, union: { type: [String, Number], required: true },
literalUnion: { type: [String, String], required: true }, literalUnion: { type: [String, String], required: true },
literalUnionMixed: { type: [String, Number, Boolean], required: true }, literalUnionMixed: { type: [String, Number, Boolean], required: true },
@ -887,6 +888,7 @@ export default _defineComponent({
recordRef: Record<string, null> recordRef: Record<string, null>
interface: Test interface: Test
alias: Alias alias: Alias
method(): void
union: string | number union: string | number
literalUnion: 'foo' | 'bar' literalUnion: 'foo' | 'bar'

View File

@ -532,6 +532,7 @@ const emit = defineEmits(['a', 'b'])
recordRef: Record<string, null> recordRef: Record<string, null>
interface: Test interface: Test
alias: Alias alias: Alias
method(): void
union: string | number union: string | number
literalUnion: 'foo' | 'bar' literalUnion: 'foo' | 'bar'
@ -557,6 +558,7 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(`recordRef: { type: Object, required: true }`) expect(content).toMatch(`recordRef: { type: Object, required: true }`)
expect(content).toMatch(`interface: { type: Object, required: true }`) expect(content).toMatch(`interface: { type: Object, required: true }`)
expect(content).toMatch(`alias: { type: Array, required: true }`) expect(content).toMatch(`alias: { type: Array, required: true }`)
expect(content).toMatch(`method: { type: Function, required: true }`)
expect(content).toMatch( expect(content).toMatch(
`union: { type: [String, Number], required: true }` `union: { type: [String, Number], required: true }`
) )
@ -585,6 +587,7 @@ const emit = defineEmits(['a', 'b'])
recordRef: BindingTypes.PROPS, recordRef: BindingTypes.PROPS,
interface: BindingTypes.PROPS, interface: BindingTypes.PROPS,
alias: BindingTypes.PROPS, alias: BindingTypes.PROPS,
method: BindingTypes.PROPS,
union: BindingTypes.PROPS, union: BindingTypes.PROPS,
literalUnion: BindingTypes.PROPS, literalUnion: BindingTypes.PROPS,
literalUnionMixed: BindingTypes.PROPS, literalUnionMixed: BindingTypes.PROPS,

View File

@ -1355,14 +1355,25 @@ function extractRuntimeProps(
) { ) {
const members = node.type === 'TSTypeLiteral' ? node.members : node.body const members = node.type === 'TSTypeLiteral' ? node.members : node.body
for (const m of members) { for (const m of members) {
if (m.type === 'TSPropertySignature' && m.key.type === 'Identifier') { if (
(m.type === 'TSPropertySignature' || m.type === 'TSMethodSignature') &&
m.key.type === 'Identifier'
) {
let type
if (__DEV__) {
if (m.type === 'TSMethodSignature') {
type = ['Function']
} else if (m.typeAnnotation) {
type = inferRuntimeType(
m.typeAnnotation.typeAnnotation,
declaredTypes
)
}
}
props[m.key.name] = { props[m.key.name] = {
key: m.key.name, key: m.key.name,
required: !m.optional, required: !m.optional,
type: type: type || [`null`]
__DEV__ && m.typeAnnotation
? inferRuntimeType(m.typeAnnotation.typeAnnotation, declaredTypes)
: [`null`]
} }
} }
} }