Skip to content

Commit

Permalink
fix(types): emit type in SetupContext (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerAlbertCom committed Jan 16, 2022
1 parent 315f6ab commit 5c35403
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/component/componentOptions.ts
Expand Up @@ -22,10 +22,14 @@ export interface MethodOptions {
[key: string]: Function
}

export type SetupFunction<Props, RawBindings = {}> = (
export type SetupFunction<
Props,
RawBindings = {},
Emits extends EmitsOptions = {}
> = (
this: void,
props: Readonly<Props>,
ctx: SetupContext
ctx: SetupContext<Emits>
) => RawBindings | (() => VNode | null) | void

interface ComponentOptionsBase<
Expand Down Expand Up @@ -70,7 +74,7 @@ export type ComponentOptionsWithProps<
> = ComponentOptionsBase<Props, D, C, M> & {
props?: PropsOptions
emits?: Emits & ThisType<void>
setup?: SetupFunction<Props, RawBindings>
setup?: SetupFunction<Props, RawBindings, Emits>
} & ThisType<
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits>
>
Expand All @@ -88,7 +92,7 @@ export type ComponentOptionsWithArrayProps<
> = ComponentOptionsBase<Props, D, C, M> & {
props?: PropNames[]
emits?: Emits & ThisType<void>
setup?: SetupFunction<Props, RawBindings>
setup?: SetupFunction<Props, RawBindings, Emits>
} & ThisType<
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits>
>
Expand All @@ -105,7 +109,7 @@ export type ComponentOptionsWithoutProps<
> = ComponentOptionsBase<Props, D, C, M> & {
props?: undefined
emits?: Emits & ThisType<void>
setup?: SetupFunction<Props, RawBindings>
setup?: SetupFunction<Props, RawBindings, Emits>
} & ThisType<
ComponentRenderProxy<Props, RawBindings, D, C, M, Mixin, Extends, Emits>
>
Expand Down
2 changes: 1 addition & 1 deletion src/runtimeContext.ts
Expand Up @@ -145,7 +145,7 @@ export type ComponentRenderEmitFn<

export type Slots = Readonly<InternalSlots>

export interface SetupContext<E = EmitsOptions> {
export interface SetupContext<E extends EmitsOptions = {}> {
attrs: Data
slots: Slots
emit: EmitFn<E>
Expand Down
4 changes: 4 additions & 0 deletions test-dts/defineComponent-vue2.d.tsx
Expand Up @@ -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)
Expand Down

0 comments on commit 5c35403

Please sign in to comment.