From 368b6a163b874a45600d386ca46d6a7097763b4d Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 27 Mar 2023 17:56:08 +0800 Subject: [PATCH 1/3] feat(types): `defineComponent()` with generics support BREAKING CHANGE: The type of `defineComponent()` when passing in a function has changed. This overload signature was rarely used in the past, so it is changed to something more useful. Previously the return type was `DefineComponent`, now it is a function type that inherits the generics of the function passed in. The function passed in as the first argument now also requires a return type of a render function, as the signature is no longer meant to be used for other use cases. --- packages/dts-test/defineComponent.test-d.tsx | 115 +++++++++++++++++- packages/dts-test/h.test-d.ts | 2 +- .../runtime-core/__tests__/apiOptions.spec.ts | 30 ++--- .../runtime-core/src/apiDefineComponent.ts | 49 ++++++-- 4 files changed, 168 insertions(+), 28 deletions(-) diff --git a/packages/dts-test/defineComponent.test-d.tsx b/packages/dts-test/defineComponent.test-d.tsx index 522b6a8daae..8affd03c3d3 100644 --- a/packages/dts-test/defineComponent.test-d.tsx +++ b/packages/dts-test/defineComponent.test-d.tsx @@ -351,7 +351,7 @@ describe('type inference w/ optional props declaration', () => { }) describe('type inference w/ direct setup function', () => { - const MyComponent = defineComponent((_props: { msg: string }) => {}) + const MyComponent = defineComponent((_props: { msg: string }) => () => {}) expectType() // @ts-expect-error ; @@ -1250,10 +1250,121 @@ describe('prop starting with `on*` is broken', () => { }) }) +describe('function syntax w/ generics', () => { + const Comp = defineComponent( + // TODO: babel plugin to auto infer runtime props options from type + // similar to defineProps<{...}>() + (props: { msg: T; list: T[] }) => { + // use Composition API here like in