Skip to content

Commit cf5a5e0

Browse files
authored
fix(compiler-sfc): improve type inference for TSTypeAliasDeclaration with better runtime type detection (#13245)
close #13240
1 parent d37a2ac commit cf5a5e0

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ export default /*@__PURE__*/_defineComponent({
148148
149149
150150
151+
return { }
152+
}
153+
154+
})"
155+
`;
156+
157+
exports[`defineProps > w/ TSTypeAliasDeclaration 1`] = `
158+
"import { defineComponent as _defineComponent } from 'vue'
159+
type FunFoo<O> = (item: O) => boolean;
160+
type FunBar = FunFoo<number>;
161+
162+
export default /*@__PURE__*/_defineComponent({
163+
props: {
164+
foo: { type: Function, required: false, default: () => true },
165+
bar: { type: Function, required: false, default: () => true }
166+
},
167+
setup(__props: any, { expose: __expose }) {
168+
__expose();
169+
170+
171+
151172
return { }
152173
}
153174

packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,4 +808,30 @@ const props = defineProps({ foo: String })
808808
expect(content).toMatch(`foo: { default: 5.5, type: Number }`)
809809
assertCode(content)
810810
})
811+
812+
test('w/ TSTypeAliasDeclaration', () => {
813+
const { content } = compile(`
814+
<script setup lang="ts">
815+
type FunFoo<O> = (item: O) => boolean;
816+
type FunBar = FunFoo<number>;
817+
withDefaults(
818+
defineProps<{
819+
foo?: FunFoo<number>;
820+
bar?: FunBar;
821+
}>(),
822+
{
823+
foo: () => true,
824+
bar: () => true,
825+
},
826+
);
827+
</script>
828+
`)
829+
assertCode(content)
830+
expect(content).toMatch(
831+
`foo: { type: Function, required: false, default: () => true }`,
832+
)
833+
expect(content).toMatch(
834+
`bar: { type: Function, required: false, default: () => true }`,
835+
)
836+
})
811837
})

packages/compiler-sfc/src/script/resolveType.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,14 @@ export function inferRuntimeType(
15541554
case 'TSTypeReference': {
15551555
const resolved = resolveTypeReference(ctx, node, scope)
15561556
if (resolved) {
1557+
if (resolved.type === 'TSTypeAliasDeclaration') {
1558+
return inferRuntimeType(
1559+
ctx,
1560+
resolved.typeAnnotation,
1561+
resolved._ownerScope,
1562+
isKeyOf,
1563+
)
1564+
}
15571565
return inferRuntimeType(ctx, resolved, resolved._ownerScope, isKeyOf)
15581566
}
15591567

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy