Skip to content

Commit 521988d

Browse files
types(runtime-core): add OnCleanup parameter type in this.$watch (#9371)
1 parent c6a4c08 commit 521988d

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

packages/dts-test/watch.test-d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ const source = ref('foo')
1212
const source2 = computed(() => source.value)
1313
const source3 = () => 1
1414

15+
type OnCleanup = (fn: () => void) => void
16+
1517
// lazy watcher will have consistent types for oldValue.
16-
watch(source, (value, oldValue) => {
18+
watch(source, (value, oldValue, onCleanup) => {
1719
expectType<string>(value)
1820
expectType<string>(oldValue)
21+
expectType<OnCleanup>(onCleanup)
1922
})
2023

2124
watch([source, source2, source3], (values, oldValues) => {
@@ -92,9 +95,10 @@ defineComponent({
9295
created() {
9396
this.$watch(
9497
() => this.a,
95-
(v, ov) => {
98+
(v, ov, onCleanup) => {
9699
expectType<number>(v)
97100
expectType<number>(ov)
101+
expectType<OnCleanup>(onCleanup)
98102
},
99103
)
100104
},

packages/runtime-core/src/apiWatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type MapSources<T, Immediate> = {
6565
: never
6666
}
6767

68-
type OnCleanup = (cleanupFn: () => void) => void
68+
export type OnCleanup = (cleanupFn: () => void) => void
6969

7070
export interface WatchOptionsBase extends DebuggerOptions {
7171
flush?: 'pre' | 'post' | 'sync'

packages/runtime-core/src/componentPublicInstance.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from './component'
77
import { nextTick, queueJob } from './scheduler'
88
import {
9+
type OnCleanup,
910
type WatchOptions,
1011
type WatchStopHandle,
1112
instanceWatch,
@@ -229,8 +230,8 @@ export type ComponentPublicInstance<
229230
$watch<T extends string | ((...args: any) => any)>(
230231
source: T,
231232
cb: T extends (...args: any) => infer R
232-
? (...args: [R, R]) => any
233-
: (...args: any) => any,
233+
? (...args: [R, R, OnCleanup]) => any
234+
: (...args: [any, any, OnCleanup]) => any,
234235
options?: WatchOptions,
235236
): WatchStopHandle
236237
} & IfAny<P, P, Omit<P, keyof ShallowUnwrapRef<B>>> &

0 commit comments

Comments
 (0)