Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): emit type in SetupContext #884

Merged
merged 1 commit into from Jan 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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