fix(compiler-sfc): support using declared interface in normal script with defineProps() (#4522)
fix #4423
This commit is contained in:
parent
5594643d7b
commit
14d65181f1
@ -866,6 +866,26 @@ export default /*#__PURE__*/_defineComponent({
|
||||
|
||||
|
||||
|
||||
return { }
|
||||
}
|
||||
|
||||
})"
|
||||
`;
|
||||
|
||||
exports[`SFC compile <script setup> with TypeScript defineProps w/ exported interface in normal script 1`] = `
|
||||
"import { defineComponent as _defineComponent } from 'vue'
|
||||
|
||||
export interface Props { x?: number }
|
||||
|
||||
export default /*#__PURE__*/_defineComponent({
|
||||
props: {
|
||||
x: { type: Number, required: false }
|
||||
},
|
||||
setup(__props: any, { expose }) {
|
||||
expose()
|
||||
|
||||
|
||||
|
||||
return { }
|
||||
}
|
||||
|
||||
|
@ -784,6 +784,22 @@ const emit = defineEmits(['a', 'b'])
|
||||
})
|
||||
})
|
||||
|
||||
test('defineProps w/ exported interface in normal script', () => {
|
||||
const { content, bindings } = compile(`
|
||||
<script lang="ts">
|
||||
export interface Props { x?: number }
|
||||
</script>
|
||||
<script setup lang="ts">
|
||||
defineProps<Props>()
|
||||
</script>
|
||||
`)
|
||||
assertCode(content)
|
||||
expect(content).toMatch(`x: { type: Number, required: false }`)
|
||||
expect(bindings).toStrictEqual({
|
||||
x: BindingTypes.PROPS
|
||||
})
|
||||
})
|
||||
|
||||
test('defineProps w/ type alias', () => {
|
||||
const { content, bindings } = compile(`
|
||||
<script setup lang="ts">
|
||||
|
@ -467,8 +467,10 @@ export function compileScript(
|
||||
return isQualifiedType(node.declaration)
|
||||
}
|
||||
}
|
||||
|
||||
for (const node of scriptSetupAst.body) {
|
||||
const body = scriptAst
|
||||
? [...scriptSetupAst.body, ...scriptAst.body]
|
||||
: scriptSetupAst.body
|
||||
for (const node of body) {
|
||||
const qualified = isQualifiedType(node)
|
||||
if (qualified) {
|
||||
return qualified
|
||||
@ -635,7 +637,7 @@ export function compileScript(
|
||||
}
|
||||
|
||||
// 1. process normal <script> first if it exists
|
||||
let scriptAst
|
||||
let scriptAst: Program | undefined
|
||||
if (script) {
|
||||
// import dedupe between <script> and <script setup>
|
||||
scriptAst = parse(
|
||||
|
Loading…
Reference in New Issue
Block a user