chore(types): improve type safety in watch functions and instanceWatch (#13918)

This commit is contained in:
Arthur Darkstone 2025-09-24 17:21:41 +08:00 committed by GitHub
parent 5e1e791880
commit fda47ac702
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 9 deletions

View File

@ -21,6 +21,7 @@ import { queuePostRenderEffect } from './renderer'
import { warn } from './warning' import { warn } from './warning'
import type { ObjectWatchOptionItem } from './componentOptions' import type { ObjectWatchOptionItem } from './componentOptions'
import { useSSRContext } from './helpers/useSsrContext' import { useSSRContext } from './helpers/useSsrContext'
import type { ComponentPublicInstance } from './componentPublicInstance'
export type { export type {
WatchHandle, WatchHandle,
@ -66,7 +67,9 @@ export function watchPostEffect(
return doWatch( return doWatch(
effect, effect,
null, null,
__DEV__ ? extend({}, options as any, { flush: 'post' }) : { flush: 'post' }, __DEV__
? extend({}, options as WatchEffectOptions, { flush: 'post' })
: { flush: 'post' },
) )
} }
@ -77,7 +80,9 @@ export function watchSyncEffect(
return doWatch( return doWatch(
effect, effect,
null, null,
__DEV__ ? extend({}, options as any, { flush: 'sync' }) : { flush: 'sync' }, __DEV__
? extend({}, options as WatchEffectOptions, { flush: 'sync' })
: { flush: 'sync' },
) )
} }
@ -243,11 +248,11 @@ export function instanceWatch(
value: WatchCallback | ObjectWatchOptionItem, value: WatchCallback | ObjectWatchOptionItem,
options?: WatchOptions, options?: WatchOptions,
): WatchHandle { ): WatchHandle {
const publicThis = this.proxy as any const publicThis = this.proxy
const getter = isString(source) const getter = isString(source)
? source.includes('.') ? source.includes('.')
? createPathGetter(publicThis, source) ? createPathGetter(publicThis!, source)
: () => publicThis[source] : () => publicThis![source as keyof typeof publicThis]
: source.bind(publicThis, publicThis) : source.bind(publicThis, publicThis)
let cb let cb
if (isFunction(value)) { if (isFunction(value)) {
@ -262,12 +267,15 @@ export function instanceWatch(
return res return res
} }
export function createPathGetter(ctx: any, path: string) { export function createPathGetter(
ctx: ComponentPublicInstance,
path: string,
): () => WatchSource | WatchSource[] | WatchEffect | object {
const segments = path.split('.') const segments = path.split('.')
return (): any => { return (): WatchSource | WatchSource[] | WatchEffect | object => {
let cur = ctx let cur = ctx
for (let i = 0; i < segments.length && cur; i++) { for (let i = 0; i < segments.length && cur; i++) {
cur = cur[segments[i]] cur = cur[segments[i] as keyof typeof cur]
} }
return cur return cur
} }

View File

@ -852,7 +852,7 @@ export function createWatcher(
): void { ): void {
let getter = key.includes('.') let getter = key.includes('.')
? createPathGetter(publicThis, key) ? createPathGetter(publicThis, key)
: () => (publicThis as any)[key] : () => publicThis[key as keyof typeof publicThis]
const options: WatchOptions = {} const options: WatchOptions = {}
if (__COMPAT__) { if (__COMPAT__) {