Skip to content

Commit 5c35403

Browse files
authored
fix(types): emit type in SetupContext (#884)
1 parent 315f6ab commit 5c35403

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/component/componentOptions.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ export interface MethodOptions {
2222
[key: string]: Function
2323
}
2424

25-
export type SetupFunction<Props, RawBindings = {}> = (
25+
export type SetupFunction<
26+
Props,
27+
RawBindings = {},
28+
Emits extends EmitsOptions = {}
29+
> = (
2630
this: void,
2731
props: Readonly<Props>,
28-
ctx: SetupContext
32+
ctx: SetupContext<Emits>
2933
) => RawBindings | (() => VNode | null) | void
3034

3135
interface ComponentOptionsBase<
@@ -70,7 +74,7 @@ export type ComponentOptionsWithProps<
7074
> = ComponentOptionsBase<Props, D, C, M> & {
7175
props?: PropsOptions
7276
emits?: Emits & ThisType<void>
73-
setup?: SetupFunction<Props, RawBindings>
77+
setup?: SetupFunction<Props, RawBindings, Emits>
7478
} & ThisType<
7579
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits>
7680
>
@@ -88,7 +92,7 @@ export type ComponentOptionsWithArrayProps<
8892
> = ComponentOptionsBase<Props, D, C, M> & {
8993
props?: PropNames[]
9094
emits?: Emits & ThisType<void>
91-
setup?: SetupFunction<Props, RawBindings>
95+
setup?: SetupFunction<Props, RawBindings, Emits>
9296
} & ThisType<
9397
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits>
9498
>
@@ -105,7 +109,7 @@ export type ComponentOptionsWithoutProps<
105109
> = ComponentOptionsBase<Props, D, C, M> & {
106110
props?: undefined
107111
emits?: Emits & ThisType<void>
108-
setup?: SetupFunction<Props, RawBindings>
112+
setup?: SetupFunction<Props, RawBindings, Emits>
109113
} & ThisType<
110114
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits>
111115
>

src/runtimeContext.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export type ComponentRenderEmitFn<
145145

146146
export type Slots = Readonly<InternalSlots>
147147

148-
export interface SetupContext<E = EmitsOptions> {
148+
export interface SetupContext<E extends EmitsOptions = {}> {
149149
attrs: Data
150150
slots: Slots
151151
emit: EmitFn<E>

test-dts/defineComponent-vue2.d.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ describe('emits', () => {
1414
click: (n: number) => typeof n === 'number',
1515
input: (b: string) => b.length > 1,
1616
},
17+
setup(props, { emit }) {
18+
emit('click', 1)
19+
emit('input', 'foo')
20+
},
1721
created() {
1822
this.$emit('click', 1)
1923
this.$emit('click', 1).$emit('click', 1)

0 commit comments

Comments
 (0)