From 5c3540312fd15a724cc0fea72476c96dbde1bb5b Mon Sep 17 00:00:00 2001 From: Albert Weinert Date: Sun, 16 Jan 2022 10:14:42 +0100 Subject: [PATCH] fix(types): `emit` type in SetupContext (#884) --- src/component/componentOptions.ts | 14 +++++++++----- src/runtimeContext.ts | 2 +- test-dts/defineComponent-vue2.d.tsx | 4 ++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/component/componentOptions.ts b/src/component/componentOptions.ts index dd3de46b..a2eb4eaf 100644 --- a/src/component/componentOptions.ts +++ b/src/component/componentOptions.ts @@ -22,10 +22,14 @@ export interface MethodOptions { [key: string]: Function } -export type SetupFunction = ( +export type SetupFunction< + Props, + RawBindings = {}, + Emits extends EmitsOptions = {} +> = ( this: void, props: Readonly, - ctx: SetupContext + ctx: SetupContext ) => RawBindings | (() => VNode | null) | void interface ComponentOptionsBase< @@ -70,7 +74,7 @@ export type ComponentOptionsWithProps< > = ComponentOptionsBase & { props?: PropsOptions emits?: Emits & ThisType - setup?: SetupFunction + setup?: SetupFunction } & ThisType< ComponentRenderProxy > @@ -88,7 +92,7 @@ export type ComponentOptionsWithArrayProps< > = ComponentOptionsBase & { props?: PropNames[] emits?: Emits & ThisType - setup?: SetupFunction + setup?: SetupFunction } & ThisType< ComponentRenderProxy > @@ -105,7 +109,7 @@ export type ComponentOptionsWithoutProps< > = ComponentOptionsBase & { props?: undefined emits?: Emits & ThisType - setup?: SetupFunction + setup?: SetupFunction } & ThisType< ComponentRenderProxy > diff --git a/src/runtimeContext.ts b/src/runtimeContext.ts index 8b4cbbeb..e076eac2 100644 --- a/src/runtimeContext.ts +++ b/src/runtimeContext.ts @@ -145,7 +145,7 @@ export type ComponentRenderEmitFn< export type Slots = Readonly -export interface SetupContext { +export interface SetupContext { attrs: Data slots: Slots emit: EmitFn diff --git a/test-dts/defineComponent-vue2.d.tsx b/test-dts/defineComponent-vue2.d.tsx index bb27617f..7870c555 100644 --- a/test-dts/defineComponent-vue2.d.tsx +++ b/test-dts/defineComponent-vue2.d.tsx @@ -14,6 +14,10 @@ describe('emits', () => { click: (n: number) => typeof n === 'number', input: (b: string) => b.length > 1, }, + setup(props, { emit }) { + emit('click', 1) + emit('input', 'foo') + }, created() { this.$emit('click', 1) this.$emit('click', 1).$emit('click', 1)