mirror of https://github.com/vuejs/core.git
fix(compiler-sfc): improve type inference for TSTypeAliasDeclaration with better runtime type detection (#13245)
close #13240
This commit is contained in:
parent
d37a2ac59d
commit
cf5a5e0edf
|
@ -148,6 +148,27 @@ export default /*@__PURE__*/_defineComponent({
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return { }
|
||||||
|
}
|
||||||
|
|
||||||
|
})"
|
||||||
|
`;
|
||||||
|
|
||||||
|
exports[`defineProps > w/ TSTypeAliasDeclaration 1`] = `
|
||||||
|
"import { defineComponent as _defineComponent } from 'vue'
|
||||||
|
type FunFoo<O> = (item: O) => boolean;
|
||||||
|
type FunBar = FunFoo<number>;
|
||||||
|
|
||||||
|
export default /*@__PURE__*/_defineComponent({
|
||||||
|
props: {
|
||||||
|
foo: { type: Function, required: false, default: () => true },
|
||||||
|
bar: { type: Function, required: false, default: () => true }
|
||||||
|
},
|
||||||
|
setup(__props: any, { expose: __expose }) {
|
||||||
|
__expose();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return { }
|
return { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -808,4 +808,30 @@ const props = defineProps({ foo: String })
|
||||||
expect(content).toMatch(`foo: { default: 5.5, type: Number }`)
|
expect(content).toMatch(`foo: { default: 5.5, type: Number }`)
|
||||||
assertCode(content)
|
assertCode(content)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('w/ TSTypeAliasDeclaration', () => {
|
||||||
|
const { content } = compile(`
|
||||||
|
<script setup lang="ts">
|
||||||
|
type FunFoo<O> = (item: O) => boolean;
|
||||||
|
type FunBar = FunFoo<number>;
|
||||||
|
withDefaults(
|
||||||
|
defineProps<{
|
||||||
|
foo?: FunFoo<number>;
|
||||||
|
bar?: FunBar;
|
||||||
|
}>(),
|
||||||
|
{
|
||||||
|
foo: () => true,
|
||||||
|
bar: () => true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
</script>
|
||||||
|
`)
|
||||||
|
assertCode(content)
|
||||||
|
expect(content).toMatch(
|
||||||
|
`foo: { type: Function, required: false, default: () => true }`,
|
||||||
|
)
|
||||||
|
expect(content).toMatch(
|
||||||
|
`bar: { type: Function, required: false, default: () => true }`,
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1554,6 +1554,14 @@ export function inferRuntimeType(
|
||||||
case 'TSTypeReference': {
|
case 'TSTypeReference': {
|
||||||
const resolved = resolveTypeReference(ctx, node, scope)
|
const resolved = resolveTypeReference(ctx, node, scope)
|
||||||
if (resolved) {
|
if (resolved) {
|
||||||
|
if (resolved.type === 'TSTypeAliasDeclaration') {
|
||||||
|
return inferRuntimeType(
|
||||||
|
ctx,
|
||||||
|
resolved.typeAnnotation,
|
||||||
|
resolved._ownerScope,
|
||||||
|
isKeyOf,
|
||||||
|
)
|
||||||
|
}
|
||||||
return inferRuntimeType(ctx, resolved, resolved._ownerScope, isKeyOf)
|
return inferRuntimeType(ctx, resolved, resolved._ownerScope, isKeyOf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue