From: 远方os Date: Mon, 27 May 2024 16:25:07 +0000 (+0800) Subject: types(runtime-core): add `OnCleanup` parameter type in `this.$watch` (#9371) X-Git-Tag: v3.4.28~67 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=521988d7e118d9aa23fb0f4f6284bf892229eb75;p=thirdparty%2Fvuejs%2Fcore.git types(runtime-core): add `OnCleanup` parameter type in `this.$watch` (#9371) --- diff --git a/packages/dts-test/watch.test-d.ts b/packages/dts-test/watch.test-d.ts index 5986d3d30b..507fb6f5dc 100644 --- a/packages/dts-test/watch.test-d.ts +++ b/packages/dts-test/watch.test-d.ts @@ -12,10 +12,13 @@ const source = ref('foo') const source2 = computed(() => source.value) const source3 = () => 1 +type OnCleanup = (fn: () => void) => void + // lazy watcher will have consistent types for oldValue. -watch(source, (value, oldValue) => { +watch(source, (value, oldValue, onCleanup) => { expectType(value) expectType(oldValue) + expectType(onCleanup) }) watch([source, source2, source3], (values, oldValues) => { @@ -92,9 +95,10 @@ defineComponent({ created() { this.$watch( () => this.a, - (v, ov) => { + (v, ov, onCleanup) => { expectType(v) expectType(ov) + expectType(onCleanup) }, ) }, diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 0fd6b050cf..ffad8ad549 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -65,7 +65,7 @@ type MapSources = { : never } -type OnCleanup = (cleanupFn: () => void) => void +export type OnCleanup = (cleanupFn: () => void) => void export interface WatchOptionsBase extends DebuggerOptions { flush?: 'pre' | 'post' | 'sync' diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 357ad280b9..52801e172a 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -6,6 +6,7 @@ import { } from './component' import { nextTick, queueJob } from './scheduler' import { + type OnCleanup, type WatchOptions, type WatchStopHandle, instanceWatch, @@ -229,8 +230,8 @@ export type ComponentPublicInstance< $watch any)>( source: T, cb: T extends (...args: any) => infer R - ? (...args: [R, R]) => any - : (...args: any) => any, + ? (...args: [R, R, OnCleanup]) => any + : (...args: [any, any, OnCleanup]) => any, options?: WatchOptions, ): WatchStopHandle } & IfAny>> &