]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): support using declared interface in normal script with defineProps...
authoredison <daiwei521@126.com>
Sun, 5 Sep 2021 22:10:15 +0000 (06:10 +0800)
committerGitHub <noreply@github.com>
Sun, 5 Sep 2021 22:10:15 +0000 (18:10 -0400)
fix #4423

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

index 430b2993f618a0e4c5aceea16687325f51ecc4d3..4292327ca681dd52bf2f011998c58b593cbd6f2d 100644 (file)
@@ -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 {  }
 }
 
index a93be902e47a4498acb6cc66b37e81140d442512..086a2bcf28e3a193edb7c79f3958e60e4491e141 100644 (file)
@@ -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">
index b42881b300e1b08acd717a4c0116b8c4f456f6db..7d8acc679748a2141411faadfa68b9d55feab249 100644 (file)
@@ -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(