Skip to content

Commit eab7604

Browse files
authoredOct 26, 2022
fix(compiler-sfc): allow type annotation for defineEmits variable (#5394)
fix #5393
1 parent 506a42a commit eab7604

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed
 

‎packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+17
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,23 @@ export default /*#__PURE__*/_defineComponent({
13091309

13101310

13111311

1312+
return { emit }
1313+
}
1314+
1315+
})"
1316+
`;
1317+
1318+
exports[`SFC compile <script setup> with TypeScript defineEmits w/ type (interface ts type) 1`] = `
1319+
"import { defineComponent as _defineComponent } from 'vue'
1320+
interface Emits { (e: 'foo'): void }
1321+
1322+
export default /*#__PURE__*/_defineComponent({
1323+
emits: ['foo'],
1324+
setup(__props, { expose, emit }) {
1325+
expose();
1326+
1327+
1328+
13121329
return { emit }
13131330
}
13141331

‎packages/compiler-sfc/__tests__/compileScript.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,19 @@ const emit = defineEmits(['a', 'b'])
11331133
expect(content).toMatch(`emits: ["foo", "bar"]`)
11341134
})
11351135

1136+
// #5393
1137+
test('defineEmits w/ type (interface ts type)', () => {
1138+
const { content } = compile(`
1139+
<script setup lang="ts">
1140+
interface Emits { (e: 'foo'): void }
1141+
const emit: Emits = defineEmits(['foo'])
1142+
</script>
1143+
`)
1144+
assertCode(content)
1145+
expect(content).toMatch(`setup(__props, { expose, emit }) {`)
1146+
expect(content).toMatch(`emits: ['foo']`)
1147+
})
1148+
11361149
test('runtime Enum', () => {
11371150
const { content, bindings } = compile(
11381151
`<script setup lang="ts">

‎packages/compiler-sfc/src/compileScript.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ export function compileScript(
553553
}
554554

555555
if (declId) {
556-
emitIdentifier = scriptSetup!.content.slice(declId.start!, declId.end!)
556+
emitIdentifier = (declId.type === 'Identifier') ? declId.name : scriptSetup!.content.slice(declId.start!, declId.end!)
557557
}
558558

559559
return true

0 commit comments

Comments
 (0)
Please sign in to comment.