From ce033d67e5d256849c38231654520ee8e845924e Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 27 Mar 2023 18:28:43 +0800 Subject: [PATCH] feat(types): `defineComponent()` with generics support (#7963) BREAKING CHANGE: The type of `defineComponent()` when passing in a function has changed. This overload signature is rarely used in practice and the breakage will be minimal, so repurposing it to something more useful should be worth it. close #3102 --- packages/dts-test/defineComponent.test-d.tsx | 124 +++++++++++++++++- packages/dts-test/h.test-d.ts | 2 +- .../runtime-core/__tests__/apiOptions.spec.ts | 46 ++++--- .../runtime-core/src/apiDefineComponent.ts | 49 +++++-- 4 files changed, 193 insertions(+), 28 deletions(-) diff --git a/packages/dts-test/defineComponent.test-d.tsx b/packages/dts-test/defineComponent.test-d.tsx index 522b6a8daae..1b981a87cb0 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,130 @@ 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