From: Evan You Date: Wed, 26 Jun 2024 00:43:24 +0000 (+0800) Subject: chore(types): reduce as any in reactivity X-Git-Tag: v3.5.0-alpha.3~14 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=912494318f692af1a69fb5f2534b8fd4f852240d;p=thirdparty%2Fvuejs%2Fcore.git chore(types): reduce as any in reactivity --- diff --git a/packages/reactivity/src/arrayInstrumentations.ts b/packages/reactivity/src/arrayInstrumentations.ts index a16eabdf72..f77c5723a0 100644 --- a/packages/reactivity/src/arrayInstrumentations.ts +++ b/packages/reactivity/src/arrayInstrumentations.ts @@ -198,7 +198,7 @@ export const arrayInstrumentations: Record = { // instrument iterators to take ARRAY_ITERATE dependency function iterator( self: unknown[], - method: keyof Array, + method: keyof Array, wrapValue: (value: any) => unknown, ) { // note that taking ARRAY_ITERATE dependency here is not strictly equivalent @@ -210,11 +210,13 @@ function iterator( // given that JS iterator can only be read once, this doesn't seem like // a plausible use-case, so this tracking simplification seems ok. const arr = shallowReadArray(self) - const iter = (arr[method] as any)() + const iter = (arr[method] as any)() as IterableIterator & { + _next: IterableIterator['next'] + } if (arr !== self && !isShallow(self)) { - ;(iter as any)._next = iter.next + iter._next = iter.next iter.next = () => { - const result = (iter as any)._next() + const result = iter._next() if (result.value) { result.value = wrapValue(result.value) } diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 9a899c9558..1b3031aaed 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -34,7 +34,7 @@ const builtInSymbols = new Set( // but accessing them on Symbol leads to TypeError because Symbol is a strict mode // function .filter(key => key !== 'arguments' && key !== 'caller') - .map(key => (Symbol as any)[key]) + .map(key => Symbol[key as keyof SymbolConstructor]) .filter(isSymbol), ) @@ -137,12 +137,12 @@ class MutableReactiveHandler extends BaseReactiveHandler { } set( - target: object, + target: Record, key: string | symbol, value: unknown, receiver: object, ): boolean { - let oldValue = (target as any)[key] + let oldValue = target[key] if (!this._isShallow) { const isOldValueReadonly = isReadonly(oldValue) if (!isShallow(value) && !isReadonly(value)) { @@ -177,9 +177,12 @@ class MutableReactiveHandler extends BaseReactiveHandler { return result } - deleteProperty(target: object, key: string | symbol): boolean { + deleteProperty( + target: Record, + key: string | symbol, + ): boolean { const hadKey = hasOwn(target, key) - const oldValue = (target as any)[key] + const oldValue = target[key] const result = Reflect.deleteProperty(target, key) if (result && hadKey) { trigger(target, TriggerOpTypes.DELETE, key, undefined, oldValue) @@ -187,14 +190,15 @@ class MutableReactiveHandler extends BaseReactiveHandler { return result } - has(target: object, key: string | symbol): boolean { + has(target: Record, key: string | symbol): boolean { const result = Reflect.has(target, key) if (!isSymbol(key) || !builtInSymbols.has(key)) { track(target, TrackOpTypes.HAS, key) } return result } - ownKeys(target: object): (string | symbol)[] { + + ownKeys(target: Record): (string | symbol)[] { track( target, TrackOpTypes.ITERATE, diff --git a/packages/reactivity/src/collectionHandlers.ts b/packages/reactivity/src/collectionHandlers.ts index 629868edb0..9e05ee4800 100644 --- a/packages/reactivity/src/collectionHandlers.ts +++ b/packages/reactivity/src/collectionHandlers.ts @@ -1,4 +1,4 @@ -import { toRaw, toReactive, toReadonly } from './reactive' +import { type Target, toRaw, toReactive, toReadonly } from './reactive' import { ITERATE_KEY, MAP_KEY_ITERATE_KEY, track, trigger } from './dep' import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants' import { capitalize, hasChanged, hasOwn, isMap, toRawType } from '@vue/shared' @@ -6,10 +6,10 @@ import { warn } from './warning' type CollectionTypes = IterableCollections | WeakCollections -type IterableCollections = Map | Set -type WeakCollections = WeakMap | WeakSet -type MapTypes = Map | WeakMap -type SetTypes = Set | WeakSet +type IterableCollections = (Map | Set) & Target +type WeakCollections = (WeakMap | WeakSet) & Target +type MapTypes = (Map | WeakMap) & Target +type SetTypes = (Set | WeakSet) & Target const toShallow = (value: T): T => value @@ -24,7 +24,7 @@ function get( ) { // #1772: readonly(reactive(Map)) should return readonly + reactive version // of the value - target = (target as any)[ReactiveFlags.RAW] + target = target[ReactiveFlags.RAW] const rawTarget = toRaw(target) const rawKey = toRaw(key) if (!isReadonly) { @@ -47,7 +47,7 @@ function get( } function has(this: CollectionTypes, key: unknown, isReadonly = false): boolean { - const target = (this as any)[ReactiveFlags.RAW] + const target = this[ReactiveFlags.RAW] const rawTarget = toRaw(target) const rawKey = toRaw(key) if (!isReadonly) { @@ -62,7 +62,7 @@ function has(this: CollectionTypes, key: unknown, isReadonly = false): boolean { } function size(target: IterableCollections, isReadonly = false) { - target = (target as any)[ReactiveFlags.RAW] + target = target[ReactiveFlags.RAW] !isReadonly && track(toRaw(target), TrackOpTypes.ITERATE, ITERATE_KEY) return Reflect.get(target, 'size', target) } @@ -144,7 +144,7 @@ function createForEach(isReadonly: boolean, isShallow: boolean) { callback: Function, thisArg?: unknown, ) { - const observed = this as any + const observed = this const target = observed[ReactiveFlags.RAW] const rawTarget = toRaw(target) const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive @@ -180,7 +180,7 @@ function createIterableMethod( this: IterableCollections, ...args: unknown[] ): Iterable & Iterator { - const target = (this as any)[ReactiveFlags.RAW] + const target = this[ReactiveFlags.RAW] const rawTarget = toRaw(target) const targetIsMap = isMap(rawTarget) const isPair =