Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): infer props type when TS interface function #7119

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,7 @@ export default /*#__PURE__*/_defineComponent({
alias: { type: Array, required: true },
method: { type: Function, required: true },
symbol: { type: Symbol, required: true },
objectOrFn: { type: [Function, Object], required: true },
union: { type: [String, Number], required: true },
literalUnion: { type: String, required: true },
literalUnionNumber: { type: Number, required: true },
Expand Down
8 changes: 8 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,10 @@ const emit = defineEmits(['a', 'b'])
alias: Alias
method(): void
symbol: symbol
objectOrFn: {
(): void
foo: string
}

union: string | number
literalUnion: 'foo' | 'bar'
Expand Down Expand Up @@ -928,6 +932,9 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(`alias: { type: Array, required: true }`)
expect(content).toMatch(`method: { type: Function, required: true }`)
expect(content).toMatch(`symbol: { type: Symbol, required: true }`)
expect(content).toMatch(
`objectOrFn: { type: [Function, Object], required: true },`
)
expect(content).toMatch(
`union: { type: [String, Number], required: true }`
)
Expand Down Expand Up @@ -961,6 +968,7 @@ const emit = defineEmits(['a', 'b'])
alias: BindingTypes.PROPS,
method: BindingTypes.PROPS,
symbol: BindingTypes.PROPS,
objectOrFn: BindingTypes.PROPS,
union: BindingTypes.PROPS,
literalUnion: BindingTypes.PROPS,
literalUnionNumber: BindingTypes.PROPS,
Expand Down
16 changes: 14 additions & 2 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1920,9 +1920,21 @@ function inferRuntimeType(
return ['Boolean']
case 'TSObjectKeyword':
return ['Object']
case 'TSTypeLiteral':
case 'TSTypeLiteral': {
// TODO (nice to have) generate runtime property validation
return ['Object']
const types = new Set<string>()
for (const m of node.members) {
switch (m.type) {
case 'TSCallSignatureDeclaration':
case 'TSConstructSignatureDeclaration':
types.add('Function')
break
default:
types.add('Object')
}
}
return Array.from(types)
}
case 'TSFunctionType':
return ['Function']
case 'TSArrayType':
Expand Down