Skip to content

Commit

Permalink
fix(compiler-sfc): allow type annotation for defineEmits variable (#5394
Browse files Browse the repository at this point in the history
)

fix #5393
  • Loading branch information
ygj6 authored Oct 26, 2022
1 parent 506a42a commit eab7604
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
13 changes: 13 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit eab7604

Please sign in to comment.