Skip to content

Commit afdd2f2

Browse files
committedJun 28, 2021
fix(compiler-sfc): support method signature in defineProps
fix #2983
1 parent 2f91db3 commit afdd2f2

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed
 

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

+2
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ export default _defineComponent({
864864
recordRef: { type: Object, required: true },
865865
interface: { type: Object, required: true },
866866
alias: { type: Array, required: true },
867+
method: { type: Function, required: true },
867868
union: { type: [String, Number], required: true },
868869
literalUnion: { type: [String, String], required: true },
869870
literalUnionMixed: { type: [String, Number, Boolean], required: true },
@@ -887,6 +888,7 @@ export default _defineComponent({
887888
recordRef: Record<string, null>
888889
interface: Test
889890
alias: Alias
891+
method(): void
890892
891893
union: string | number
892894
literalUnion: 'foo' | 'bar'

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

+3
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ const emit = defineEmits(['a', 'b'])
532532
recordRef: Record<string, null>
533533
interface: Test
534534
alias: Alias
535+
method(): void
535536
536537
union: string | number
537538
literalUnion: 'foo' | 'bar'
@@ -557,6 +558,7 @@ const emit = defineEmits(['a', 'b'])
557558
expect(content).toMatch(`recordRef: { type: Object, required: true }`)
558559
expect(content).toMatch(`interface: { type: Object, required: true }`)
559560
expect(content).toMatch(`alias: { type: Array, required: true }`)
561+
expect(content).toMatch(`method: { type: Function, required: true }`)
560562
expect(content).toMatch(
561563
`union: { type: [String, Number], required: true }`
562564
)
@@ -585,6 +587,7 @@ const emit = defineEmits(['a', 'b'])
585587
recordRef: BindingTypes.PROPS,
586588
interface: BindingTypes.PROPS,
587589
alias: BindingTypes.PROPS,
590+
method: BindingTypes.PROPS,
588591
union: BindingTypes.PROPS,
589592
literalUnion: BindingTypes.PROPS,
590593
literalUnionMixed: BindingTypes.PROPS,

‎packages/compiler-sfc/src/compileScript.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1355,14 +1355,25 @@ function extractRuntimeProps(
13551355
) {
13561356
const members = node.type === 'TSTypeLiteral' ? node.members : node.body
13571357
for (const m of members) {
1358-
if (m.type === 'TSPropertySignature' && m.key.type === 'Identifier') {
1358+
if (
1359+
(m.type === 'TSPropertySignature' || m.type === 'TSMethodSignature') &&
1360+
m.key.type === 'Identifier'
1361+
) {
1362+
let type
1363+
if (__DEV__) {
1364+
if (m.type === 'TSMethodSignature') {
1365+
type = ['Function']
1366+
} else if (m.typeAnnotation) {
1367+
type = inferRuntimeType(
1368+
m.typeAnnotation.typeAnnotation,
1369+
declaredTypes
1370+
)
1371+
}
1372+
}
13591373
props[m.key.name] = {
13601374
key: m.key.name,
13611375
required: !m.optional,
1362-
type:
1363-
__DEV__ && m.typeAnnotation
1364-
? inferRuntimeType(m.typeAnnotation.typeAnnotation, declaredTypes)
1365-
: [`null`]
1376+
type: type || [`null`]
13661377
}
13671378
}
13681379
}

0 commit comments

Comments
 (0)
Please sign in to comment.