Skip to content

Commit fda47ac

Browse files
chore(types): improve type safety in watch functions and instanceWatch (#13918)
1 parent 5e1e791 commit fda47ac

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

packages/runtime-core/src/apiWatch.ts

Lines changed: 16 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 type { ComponentPublicInstance } from './componentPublicInstance'
2425

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

@@ -77,7 +80,9 @@ export function watchSyncEffect(
7780
return doWatch(
7881
effect,
7982
null,
80-
__DEV__ ? extend({}, options as any, { flush: 'sync' }) : { flush: 'sync' },
83+
__DEV__
84+
? extend({}, options as WatchEffectOptions, { flush: 'sync' })
85+
: { flush: 'sync' },
8186
)
8287
}
8388

@@ -243,11 +248,11 @@ export function instanceWatch(
243248
value: WatchCallback | ObjectWatchOptionItem,
244249
options?: WatchOptions,
245250
): WatchHandle {
246-
const publicThis = this.proxy as any
251+
const publicThis = this.proxy
247252
const getter = isString(source)
248253
? source.includes('.')
249-
? createPathGetter(publicThis, source)
250-
: () => publicThis[source]
254+
? createPathGetter(publicThis!, source)
255+
: () => publicThis![source as keyof typeof publicThis]
251256
: source.bind(publicThis, publicThis)
252257
let cb
253258
if (isFunction(value)) {
@@ -262,12 +267,15 @@ export function instanceWatch(
262267
return res
263268
}
264269

265-
export function createPathGetter(ctx: any, path: string) {
270+
export function createPathGetter(
271+
ctx: ComponentPublicInstance,
272+
path: string,
273+
): () => WatchSource | WatchSource[] | WatchEffect | object {
266274
const segments = path.split('.')
267-
return (): any => {
275+
return (): WatchSource | WatchSource[] | WatchEffect | object => {
268276
let cur = ctx
269277
for (let i = 0; i < segments.length && cur; i++) {
270-
cur = cur[segments[i]]
278+
cur = cur[segments[i] as keyof typeof cur]
271279
}
272280
return cur
273281
}

packages/runtime-core/src/componentOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ export function createWatcher(
852852
): void {
853853
let getter = key.includes('.')
854854
? createPathGetter(publicThis, key)
855-
: () => (publicThis as any)[key]
855+
: () => publicThis[key as keyof typeof publicThis]
856856

857857
const options: WatchOptions = {}
858858
if (__COMPAT__) {

0 commit comments

Comments
 (0)