Skip to content

Commit 14d6518

Browse files
authored
fix(compiler-sfc): support using declared interface in normal script with defineProps() (#4522)
fix #4423
1 parent 5594643 commit 14d6518

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

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

+20
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,26 @@ export default /*#__PURE__*/_defineComponent({
866866
867867
868868
869+
return { }
870+
}
871+
872+
})"
873+
`;
874+
875+
exports[`SFC compile <script setup> with TypeScript defineProps w/ exported interface in normal script 1`] = `
876+
"import { defineComponent as _defineComponent } from 'vue'
877+
878+
export interface Props { x?: number }
879+
880+
export default /*#__PURE__*/_defineComponent({
881+
props: {
882+
x: { type: Number, required: false }
883+
},
884+
setup(__props: any, { expose }) {
885+
expose()
886+
887+
888+
869889
return { }
870890
}
871891

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

+16
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,22 @@ const emit = defineEmits(['a', 'b'])
784784
})
785785
})
786786

787+
test('defineProps w/ exported interface in normal script', () => {
788+
const { content, bindings } = compile(`
789+
<script lang="ts">
790+
export interface Props { x?: number }
791+
</script>
792+
<script setup lang="ts">
793+
defineProps<Props>()
794+
</script>
795+
`)
796+
assertCode(content)
797+
expect(content).toMatch(`x: { type: Number, required: false }`)
798+
expect(bindings).toStrictEqual({
799+
x: BindingTypes.PROPS
800+
})
801+
})
802+
787803
test('defineProps w/ type alias', () => {
788804
const { content, bindings } = compile(`
789805
<script setup lang="ts">

packages/compiler-sfc/src/compileScript.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,10 @@ export function compileScript(
467467
return isQualifiedType(node.declaration)
468468
}
469469
}
470-
471-
for (const node of scriptSetupAst.body) {
470+
const body = scriptAst
471+
? [...scriptSetupAst.body, ...scriptAst.body]
472+
: scriptSetupAst.body
473+
for (const node of body) {
472474
const qualified = isQualifiedType(node)
473475
if (qualified) {
474476
return qualified
@@ -635,7 +637,7 @@ export function compileScript(
635637
}
636638

637639
// 1. process normal <script> first if it exists
638-
let scriptAst
640+
let scriptAst: Program | undefined
639641
if (script) {
640642
// import dedupe between <script> and <script setup>
641643
scriptAst = parse(

0 commit comments

Comments
 (0)