]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(type): infer parent as `this` on `nextTick` function (#3608)
authorCarlos Rodrigues <david-181@hotmail.com>
Thu, 15 Jul 2021 20:28:20 +0000 (21:28 +0100)
committerGitHub <noreply@github.com>
Thu, 15 Jul 2021 20:28:20 +0000 (16:28 -0400)
fix #3599

packages/runtime-core/src/scheduler.ts
test-dts/defineComponent.test-d.tsx

index adaf3c26e841aebf5ad3b3ae625efb7ab4a6899d..2dcb84973e808df597c306105e39c144b0a2da7e 100644 (file)
@@ -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'
@@ -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
index d4c0b94c30ef3c7cda7af086f5d6b473862a1b44..3f59bd6ad397fed9fffeaac963bf9093948e5ffa 100644 (file)
@@ -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))
+      })
     }
   })