Skip to content

Commit e4d74e9

Browse files
fix(apiWatch): improve type safety in watch functions and instanceWatch
1 parent b555f02 commit e4d74e9

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

packages/runtime-core/src/apiWatch.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { queuePostRenderEffect } from './renderer'
2121
import { warn } from './warning'
2222
import type { ObjectWatchOptionItem } from './componentOptions'
2323
import { useSSRContext } from './helpers/useSsrContext'
24+
import { ComponentPublicInstance } from '@vue/runtime-core'
2425

2526
export type {
2627
WatchHandle,
@@ -66,7 +67,7 @@ export function watchPostEffect(
6667
return doWatch(
6768
effect,
6869
null,
69-
__DEV__ ? extend({}, options as any, { flush: 'post' }) : { flush: 'post' },
70+
__DEV__ ? extend({}, options as WatchEffectOptions, { flush: 'post' }) : { flush: 'post' },
7071
)
7172
}
7273

@@ -77,7 +78,7 @@ export function watchSyncEffect(
7778
return doWatch(
7879
effect,
7980
null,
80-
__DEV__ ? extend({}, options as any, { flush: 'sync' }) : { flush: 'sync' },
81+
__DEV__ ? extend({}, options as WatchEffectOptions, { flush: 'sync' }) : { flush: 'sync' },
8182
)
8283
}
8384

@@ -243,11 +244,11 @@ export function instanceWatch(
243244
value: WatchCallback | ObjectWatchOptionItem,
244245
options?: WatchOptions,
245246
): WatchHandle {
246-
const publicThis = this.proxy as any
247+
const publicThis = this.proxy
247248
const getter = isString(source)
248249
? source.includes('.')
249-
? createPathGetter(publicThis, source)
250-
: () => publicThis[source]
250+
? createPathGetter(publicThis!, source)
251+
: () => publicThis![source as keyof typeof publicThis]
251252
: source.bind(publicThis, publicThis)
252253
let cb
253254
if (isFunction(value)) {
@@ -262,12 +263,12 @@ export function instanceWatch(
262263
return res
263264
}
264265

265-
export function createPathGetter(ctx: any, path: string) {
266+
export function createPathGetter(ctx: ComponentPublicInstance , path: string): WatchSource | WatchSource[] | WatchEffect | object {
266267
const segments = path.split('.')
267-
return (): any => {
268+
return (): WatchSource | WatchSource[] | WatchEffect | object => {
268269
let cur = ctx
269270
for (let i = 0; i < segments.length && cur; i++) {
270-
cur = cur[segments[i]]
271+
cur = cur[segments[i] as keyof typeof cur]
271272
}
272273
return cur
273274
}

0 commit comments

Comments
 (0)