Skip to content

fix(compiler-sfc): support resolve extends interface for defineEmits #8470

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

Merged
merged 10 commits into from
Nov 10, 2023
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