Skip to content
Permalink
Browse files
fix(compiler-sfc): allow type annotation for defineEmits variable (#5394
)

fix #5393
  • Loading branch information
ygj6 committed Oct 26, 2022
1 parent 506a42a commit eab7604
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
@@ -1309,6 +1309,23 @@ export default /*#__PURE__*/_defineComponent({



return { emit }
}

})"
`;

exports[`SFC compile <script setup> with TypeScript defineEmits w/ type (interface ts type) 1`] = `
"import { defineComponent as _defineComponent } from 'vue'
interface Emits { (e: 'foo'): void }

export default /*#__PURE__*/_defineComponent({
emits: ['foo'],
setup(__props, { expose, emit }) {
expose();



return { emit }
}

@@ -1133,6 +1133,19 @@ const emit = defineEmits(['a', 'b'])
expect(content).toMatch(`emits: ["foo", "bar"]`)
})

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

test('runtime Enum', () => {
const { content, bindings } = compile(
`<script setup lang="ts">
@@ -553,7 +553,7 @@ export function compileScript(
}

if (declId) {
emitIdentifier = scriptSetup!.content.slice(declId.start!, declId.end!)
emitIdentifier = (declId.type === 'Identifier') ? declId.name : scriptSetup!.content.slice(declId.start!, declId.end!)
}

return true

0 comments on commit eab7604

Please sign in to comment.