File tree Expand file tree Collapse file tree 3 files changed +46
-2
lines changed
packages/babel-plugin-resolve-type Expand file tree Collapse file tree 3 files changed +46
-2
lines changed Original file line number Diff line number Diff line change @@ -147,14 +147,20 @@ const plugin: (
147147 if ( generics ) {
148148 ctx ! . propsTypeDecl = resolveTypeReference ( generics ) ;
149149 } else {
150- ctx ! . propsTypeDecl = getTypeAnnotation ( props . left ) ;
150+ const typeAnnotation = getTypeAnnotation ( props . left ) ;
151+ ctx ! . propsTypeDecl = typeAnnotation && t . isTSTypeReference ( typeAnnotation )
152+ ? resolveTypeReference ( typeAnnotation ) || typeAnnotation
153+ : typeAnnotation ;
151154 }
152155 ctx ! . propsRuntimeDefaults = props . right ;
153156 } else {
154157 if ( generics ) {
155158 ctx ! . propsTypeDecl = resolveTypeReference ( generics ) ;
156159 } else {
157- ctx ! . propsTypeDecl = getTypeAnnotation ( props ) ;
160+ const typeAnnotation = getTypeAnnotation ( props ) ;
161+ ctx ! . propsTypeDecl = typeAnnotation && t . isTSTypeReference ( typeAnnotation )
162+ ? resolveTypeReference ( typeAnnotation ) || typeAnnotation
163+ : typeAnnotation ;
158164 }
159165 }
160166
Original file line number Diff line number Diff line change @@ -211,6 +211,26 @@ defineComponent<Props>((props = {
211211} );"
212212`;
213213
214+ exports[`resolve type > runtime props > with type reference annotation 1`] = `
215+ "import { defineComponent } from 'vue';
216+ interface MyProps {
217+ list ?: Array < {
218+ id: number ;
219+ title : string ;
220+ }> ;
221+ }
222+ defineComponent((props: MyProps) => {
223+ return () => <div >{ props .list ? .length }< / div > ;
224+ }, {
225+ props: {
226+ list: {
227+ type: Array ,
228+ required: false
229+ }
230+ }
231+ });"
232+ `;
233+
214234exports [` resolve type > w/ tsx 1` ] = `
215235"import { type SetupContext , defineComponent } from 'vue';
216236defineComponent(() => {
Original file line number Diff line number Diff line change @@ -87,6 +87,24 @@ describe('resolve type', () => {
8787 ) ;
8888 expect ( result ) . toMatchSnapshot ( ) ;
8989 } ) ;
90+
91+ test ( 'with type reference annotation' , async ( ) => {
92+ const result = await transform (
93+ `
94+ import { defineComponent } from 'vue';
95+ interface MyProps {
96+ list?: Array<{
97+ id: number
98+ title: string
99+ }>
100+ }
101+ defineComponent((props: MyProps) => {
102+ return () => <div>{props.list?.length}</div>;
103+ })
104+ `
105+ ) ;
106+ expect ( result ) . toMatchSnapshot ( ) ;
107+ } ) ;
90108 } ) ;
91109
92110 describe ( 'runtime emits' , ( ) => {
You can’t perform that action at this time.
0 commit comments