Skip to content

Commit daae5dc

Browse files
committed
fix(compiler-sfc): TSTypeLiteral could a function
1 parent 0fbc19f commit daae5dc

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,7 @@ export default /*#__PURE__*/_defineComponent({
15851585
alias: { type: Array, required: true },
15861586
method: { type: Function, required: true },
15871587
symbol: { type: Symbol, required: true },
1588+
objectOrFn: { type: [Function, Object], required: true },
15881589
union: { type: [String, Number], required: true },
15891590
literalUnion: { type: String, required: true },
15901591
literalUnionNumber: { type: Number, required: true },

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,10 @@ const emit = defineEmits(['a', 'b'])
898898
alias: Alias
899899
method(): void
900900
symbol: symbol
901+
objectOrFn: {
902+
(): void
903+
foo: string
904+
}
901905
902906
union: string | number
903907
literalUnion: 'foo' | 'bar'
@@ -928,6 +932,9 @@ const emit = defineEmits(['a', 'b'])
928932
expect(content).toMatch(`alias: { type: Array, required: true }`)
929933
expect(content).toMatch(`method: { type: Function, required: true }`)
930934
expect(content).toMatch(`symbol: { type: Symbol, required: true }`)
935+
expect(content).toMatch(
936+
`objectOrFn: { type: [Function, Object], required: true },`
937+
)
931938
expect(content).toMatch(
932939
`union: { type: [String, Number], required: true }`
933940
)
@@ -961,6 +968,7 @@ const emit = defineEmits(['a', 'b'])
961968
alias: BindingTypes.PROPS,
962969
method: BindingTypes.PROPS,
963970
symbol: BindingTypes.PROPS,
971+
objectOrFn: BindingTypes.PROPS,
964972
union: BindingTypes.PROPS,
965973
literalUnion: BindingTypes.PROPS,
966974
literalUnionNumber: BindingTypes.PROPS,

packages/compiler-sfc/src/compileScript.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,9 +1911,21 @@ function inferRuntimeType(
19111911
return ['Boolean']
19121912
case 'TSObjectKeyword':
19131913
return ['Object']
1914-
case 'TSTypeLiteral':
1914+
case 'TSTypeLiteral': {
19151915
// TODO (nice to have) generate runtime property validation
1916-
return ['Object']
1916+
const types = new Set<string>()
1917+
for (const m of node.members) {
1918+
switch (m.type) {
1919+
case 'TSCallSignatureDeclaration':
1920+
case 'TSConstructSignatureDeclaration':
1921+
types.add('Function')
1922+
break
1923+
default:
1924+
types.add('Object')
1925+
}
1926+
}
1927+
return Array.from(types)
1928+
}
19171929
case 'TSFunctionType':
19181930
return ['Function']
19191931
case 'TSArrayType':

0 commit comments

Comments
 (0)