Skip to content

Commit aba0bd7

Browse files
authored
fix: resolve type reference in props annotation (#781)
1 parent 11b8af5 commit aba0bd7

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,21 @@ const plugin: (
163163
if (generics) {
164164
ctx!.propsTypeDecl = resolveTypeReference(generics)
165165
} else {
166-
ctx!.propsTypeDecl = getTypeAnnotation(props.left)
166+
const typeAnnotation = getTypeAnnotation(props.left)
167+
ctx!.propsTypeDecl =
168+
typeAnnotation && t.isTSTypeReference(typeAnnotation)
169+
? resolveTypeReference(typeAnnotation) || typeAnnotation
170+
: typeAnnotation
167171
}
168172
ctx!.propsRuntimeDefaults = props.right
169173
} else if (generics) {
170174
ctx!.propsTypeDecl = resolveTypeReference(generics)
171175
} else {
172-
ctx!.propsTypeDecl = getTypeAnnotation(props)
176+
const typeAnnotation = getTypeAnnotation(props)
177+
ctx!.propsTypeDecl =
178+
typeAnnotation && t.isTSTypeReference(typeAnnotation)
179+
? resolveTypeReference(typeAnnotation) || typeAnnotation
180+
: typeAnnotation
173181
}
174182

175183
if (!ctx!.propsTypeDecl) return

‎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
@@ -88,6 +88,24 @@ describe('resolve type', () => {
8888
)
8989
expect(result).toMatchSnapshot()
9090
})
91+
92+
test('with type reference annotation', async () => {
93+
const result = await transform(
94+
`
95+
import { defineComponent } from 'vue';
96+
interface MyProps {
97+
list?: Array<{
98+
id: number
99+
title: string
100+
}>
101+
}
102+
defineComponent((props: MyProps) => {
103+
return () => <div>{props.list?.length}</div>;
104+
})
105+
`,
106+
)
107+
expect(result).toMatchSnapshot()
108+
})
91109
})
92110

93111
describe('runtime emits', () => {

0 commit comments

Comments
 (0)