Skip to content

Commit 18911ab

Browse files
authored
fix(type): infer parent as this on nextTick function (#3608)
fix #3599
1 parent 08f504c commit 18911ab

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

packages/runtime-core/src/scheduler.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
22
import { isArray } from '@vue/shared'
3-
import { ComponentPublicInstance } from './componentPublicInstance'
43
import { ComponentInternalInstance, getComponentName } from './component'
54
import { warn } from './warning'
65
import { ReactiveEffect } from '@vue/reactivity'
@@ -39,9 +38,9 @@ let currentPreFlushParentJob: SchedulerJob | null = null
3938
const RECURSION_LIMIT = 100
4039
type CountMap = Map<SchedulerJob | SchedulerCb, number>
4140

42-
export function nextTick(
43-
this: ComponentPublicInstance | void,
44-
fn?: () => void
41+
export function nextTick<T = void>(
42+
this: T,
43+
fn?: (this: T) => void
4544
): Promise<void> {
4645
const p = currentFlushPromise || resolvedPromise
4746
return fn ? p.then(this ? fn.bind(this) : fn) : p

test-dts/defineComponent.test-d.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,25 @@ describe('emits', () => {
905905
expectError(this.$emit('input'))
906906
// @ts-expect-error
907907
expectError(this.$emit('input', 1))
908+
},
909+
mounted() {
910+
// #3599
911+
this.$nextTick(function() {
912+
// this should be bound to this instance
913+
914+
this.$emit('click', 1)
915+
this.$emit('input', 'foo')
916+
// @ts-expect-error
917+
expectError(this.$emit('nope'))
918+
// @ts-expect-error
919+
expectError(this.$emit('click'))
920+
// @ts-expect-error
921+
expectError(this.$emit('click', 'foo'))
922+
// @ts-expect-error
923+
expectError(this.$emit('input'))
924+
// @ts-expect-error
925+
expectError(this.$emit('input', 1))
926+
})
908927
}
909928
})
910929

0 commit comments

Comments
 (0)