Skip to content

Commit

Permalink
chore(types): reduce as any in reactivity
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 26, 2024
1 parent 7d06ca3 commit 9124943
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
10 changes: 6 additions & 4 deletions packages/reactivity/src/arrayInstrumentations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export const arrayInstrumentations: Record<string | symbol, Function> = <any>{
// instrument iterators to take ARRAY_ITERATE dependency
function iterator(
self: unknown[],
method: keyof Array<any>,
method: keyof Array<unknown>,
wrapValue: (value: any) => unknown,
) {
// note that taking ARRAY_ITERATE dependency here is not strictly equivalent
Expand All @@ -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<unknown> & {
_next: IterableIterator<unknown>['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)
}
Expand Down
18 changes: 11 additions & 7 deletions packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)

Expand Down Expand Up @@ -137,12 +137,12 @@ class MutableReactiveHandler extends BaseReactiveHandler {
}

set(
target: object,
target: Record<string | symbol, unknown>,
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)) {
Expand Down Expand Up @@ -177,24 +177,28 @@ class MutableReactiveHandler extends BaseReactiveHandler {
return result
}

deleteProperty(target: object, key: string | symbol): boolean {
deleteProperty(
target: Record<string | symbol, unknown>,
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)
}
return result
}

has(target: object, key: string | symbol): boolean {
has(target: Record<string | symbol, unknown>, 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, unknown>): (string | symbol)[] {
track(
target,
TrackOpTypes.ITERATE,
Expand Down
20 changes: 10 additions & 10 deletions packages/reactivity/src/collectionHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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'
import { warn } from './warning'

type CollectionTypes = IterableCollections | WeakCollections

type IterableCollections = Map<any, any> | Set<any>
type WeakCollections = WeakMap<any, any> | WeakSet<any>
type MapTypes = Map<any, any> | WeakMap<any, any>
type SetTypes = Set<any> | WeakSet<any>
type IterableCollections = (Map<any, any> | Set<any>) & Target
type WeakCollections = (WeakMap<any, any> | WeakSet<any>) & Target
type MapTypes = (Map<any, any> | WeakMap<any, any>) & Target
type SetTypes = (Set<any> | WeakSet<any>) & Target

const toShallow = <T extends unknown>(value: T): T => value

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down

0 comments on commit 9124943

Please sign in to comment.