Skip to content

Commit

Permalink
fix(type): infer parent as this on nextTick function (#3608)
Browse files Browse the repository at this point in the history
fix #3599
  • Loading branch information
pikax authored Jul 15, 2021
1 parent 08f504c commit 18911ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 3 additions & 4 deletions packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
import { isArray } from '@vue/shared'
import { ComponentPublicInstance } from './componentPublicInstance'
import { ComponentInternalInstance, getComponentName } from './component'
import { warn } from './warning'
import { ReactiveEffect } from '@vue/reactivity'
Expand Down Expand Up @@ -39,9 +38,9 @@ let currentPreFlushParentJob: SchedulerJob | null = null
const RECURSION_LIMIT = 100
type CountMap = Map<SchedulerJob | SchedulerCb, number>

export function nextTick(
this: ComponentPublicInstance | void,
fn?: () => void
export function nextTick<T = void>(
this: T,
fn?: (this: T) => void
): Promise<void> {
const p = currentFlushPromise || resolvedPromise
return fn ? p.then(this ? fn.bind(this) : fn) : p
Expand Down
19 changes: 19 additions & 0 deletions test-dts/defineComponent.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,25 @@ describe('emits', () => {
expectError(this.$emit('input'))
// @ts-expect-error
expectError(this.$emit('input', 1))
},
mounted() {
// #3599
this.$nextTick(function() {
// this should be bound to this instance

this.$emit('click', 1)
this.$emit('input', 'foo')
// @ts-expect-error
expectError(this.$emit('nope'))
// @ts-expect-error
expectError(this.$emit('click'))
// @ts-expect-error
expectError(this.$emit('click', 'foo'))
// @ts-expect-error
expectError(this.$emit('input'))
// @ts-expect-error
expectError(this.$emit('input', 1))
})
}
})

Expand Down

0 comments on commit 18911ab

Please sign in to comment.