Skip to content

Commit 1c0863f

Browse files
committed
fix: resolve type reference in props annotation
1 parent 85d98b5 commit 1c0863f

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

packages/babel-plugin-resolve-type/src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff 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

packages/babel-plugin-resolve-type/test/__snapshots__/resolve-type.test.tsx.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
214234
exports[`resolve type > w/ tsx 1`] = `
215235
"import { type SetupContext, defineComponent } from 'vue';
216236
defineComponent(() => {

packages/babel-plugin-resolve-type/test/resolve-type.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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', () => {

0 commit comments

Comments
 (0)