Skip to content

Commit 3a7572c

Browse files
authored
fix(compiler-sfc): infer function prop type from type literal w/ callable signature (#7119)
1 parent 701b95f commit 3a7572c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -1619,6 +1619,7 @@ export default /*#__PURE__*/_defineComponent({
16191619
alias: { type: Array, required: true },
16201620
method: { type: Function, required: true },
16211621
symbol: { type: Symbol, required: true },
1622+
objectOrFn: { type: [Function, Object], required: true },
16221623
union: { type: [String, Number], required: true },
16231624
literalUnion: { type: String, required: true },
16241625
literalUnionNumber: { type: Number, required: true },

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

+8
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,10 @@ const emit = defineEmits(['a', 'b'])
960960
alias: Alias
961961
method(): void
962962
symbol: symbol
963+
objectOrFn: {
964+
(): void
965+
foo: string
966+
}
963967
964968
union: string | number
965969
literalUnion: 'foo' | 'bar'
@@ -990,6 +994,9 @@ const emit = defineEmits(['a', 'b'])
990994
expect(content).toMatch(`alias: { type: Array, required: true }`)
991995
expect(content).toMatch(`method: { type: Function, required: true }`)
992996
expect(content).toMatch(`symbol: { type: Symbol, required: true }`)
997+
expect(content).toMatch(
998+
`objectOrFn: { type: [Function, Object], required: true },`
999+
)
9931000
expect(content).toMatch(
9941001
`union: { type: [String, Number], required: true }`
9951002
)
@@ -1023,6 +1030,7 @@ const emit = defineEmits(['a', 'b'])
10231030
alias: BindingTypes.PROPS,
10241031
method: BindingTypes.PROPS,
10251032
symbol: BindingTypes.PROPS,
1033+
objectOrFn: BindingTypes.PROPS,
10261034
union: BindingTypes.PROPS,
10271035
literalUnion: BindingTypes.PROPS,
10281036
literalUnionNumber: BindingTypes.PROPS,

packages/compiler-sfc/src/compileScript.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -2001,9 +2001,21 @@ function inferRuntimeType(
20012001
return ['Boolean']
20022002
case 'TSObjectKeyword':
20032003
return ['Object']
2004-
case 'TSTypeLiteral':
2004+
case 'TSTypeLiteral': {
20052005
// TODO (nice to have) generate runtime property validation
2006-
return ['Object']
2006+
const types = new Set<string>()
2007+
for (const m of node.members) {
2008+
switch (m.type) {
2009+
case 'TSCallSignatureDeclaration':
2010+
case 'TSConstructSignatureDeclaration':
2011+
types.add('Function')
2012+
break
2013+
default:
2014+
types.add('Object')
2015+
}
2016+
}
2017+
return Array.from(types)
2018+
}
20072019
case 'TSFunctionType':
20082020
return ['Function']
20092021
case 'TSArrayType':

0 commit comments

Comments
 (0)