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
  • Loading branch information
edison1105 committed Jun 1, 2023
1 parent a95e612 commit b01e079
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ export default /*#__PURE__*/_defineComponent({
return { emit }
}
})"
`;

exports[`defineEmits > w/ type (interface w/ entends) 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 }) {
__expose();
return { emit }
}
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 @@ -78,6 +78,18 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(`emits: ["foo", "bar"]`)
})

test('w/ type (interface w/ entends)', () => {
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
9 changes: 8 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,19 @@ 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) {
if (!base.calls) {
base.calls = calls
} else {
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 b01e079

Please sign in to comment.