Skip to content

Commit

Permalink
fix(compiler-sfc): support resolve extends interface for defineEmits (#…
Browse files Browse the repository at this point in the history
…8470)

close #8465
  • Loading branch information
edison1105 authored Nov 10, 2023
1 parent 2424013 commit 9e1b74b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ return { emit }
})"
`;

exports[`defineEmits > w/ type (interface w/ extends) 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
interface Base { (e: 'foo'): void }
interface Emits extends Base { (e: 'bar'): void }
export default /*#__PURE__*/_defineComponent({
emits: [\\"bar\\", \\"foo\\"],
setup(__props, { expose: __expose, emit: __emit }) {
__expose();
const emit = __emit
return { emit }
}
})"
`;

exports[`defineEmits > w/ type (interface) 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
interface Emits { (e: 'foo' | 'bar'): void }
Expand Down
12 changes: 12 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/defineEmits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(`emits: ["foo", "bar"]`)
})

test('w/ type (interface w/ extends)', () => {
const { content } = compile(`
<script setup lang="ts">
interface Base { (e: 'foo'): void }
interface Emits extends Base { (e: 'bar'): void }
const emit = defineEmits<Emits>()
</script>
`)
assertCode(content)
expect(content).toMatch(`emits: ["bar", "foo"]`)
})

test('w/ type (exported interface)', () => {
const { content } = compile(`
<script setup lang="ts">
Expand Down
5 changes: 4 additions & 1 deletion packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,15 @@ function resolveInterfaceMembers(
continue
}
try {
const { props } = resolveTypeElements(ctx, ext, scope)
const { props, calls } = resolveTypeElements(ctx, ext, scope)
for (const key in props) {
if (!hasOwn(base.props, key)) {
base.props[key] = props[key]
}
}
if (calls) {
;(base.calls || (base.calls = [])).push(...calls)
}
} catch (e) {
ctx.error(
`Failed to resolve extends base type.\nIf this previously worked in 3.2, ` +
Expand Down

0 comments on commit 9e1b74b

Please sign in to comment.