From 1b09cfb5a8ba644443a7f087f5dbf5e023f41255 Mon Sep 17 00:00:00 2001 From: pikax Date: Thu, 15 Apr 2021 10:00:15 +0100 Subject: [PATCH 1/2] fix(type): infer parent as `this` on `nextTick` function --- packages/runtime-core/src/scheduler.ts | 6 +++--- test-dts/defineComponent.test-d.tsx | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/src/scheduler.ts b/packages/runtime-core/src/scheduler.ts index 372866c8093..b46353da38f 100644 --- a/packages/runtime-core/src/scheduler.ts +++ b/packages/runtime-core/src/scheduler.ts @@ -49,9 +49,9 @@ let currentPreFlushParentJob: SchedulerJob | null = null const RECURSION_LIMIT = 100 type CountMap = Map -export function nextTick( - this: ComponentPublicInstance | void, - fn?: () => void +export function nextTick( + this: T, + fn?: (this: T) => void ): Promise { const p = currentFlushPromise || resolvedPromise return fn ? p.then(this ? fn.bind(this) : fn) : p diff --git a/test-dts/defineComponent.test-d.tsx b/test-dts/defineComponent.test-d.tsx index ff04a0d6f3f..d7c35d8910e 100644 --- a/test-dts/defineComponent.test-d.tsx +++ b/test-dts/defineComponent.test-d.tsx @@ -893,6 +893,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)) + }) } }) From 5063676771a0dafe5ad6fe2b5c84735b61e20fbb Mon Sep 17 00:00:00 2001 From: pikax Date: Thu, 15 Apr 2021 10:03:30 +0100 Subject: [PATCH 2/2] chore: remove unused import --- packages/runtime-core/src/scheduler.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/runtime-core/src/scheduler.ts b/packages/runtime-core/src/scheduler.ts index b46353da38f..aa2d9f26e9d 100644 --- a/packages/runtime-core/src/scheduler.ts +++ b/packages/runtime-core/src/scheduler.ts @@ -1,6 +1,5 @@ import { ErrorCodes, callWithErrorHandling } from './errorHandling' import { isArray } from '@vue/shared' -import { ComponentPublicInstance } from './componentPublicInstance' export interface SchedulerJob { (): void