Skip to content

Commit 9124943

Browse files
committed
chore(types): reduce as any in reactivity
1 parent 7d06ca3 commit 9124943

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

packages/reactivity/src/arrayInstrumentations.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export const arrayInstrumentations: Record<string | symbol, Function> = <any>{
198198
// instrument iterators to take ARRAY_ITERATE dependency
199199
function iterator(
200200
self: unknown[],
201-
method: keyof Array<any>,
201+
method: keyof Array<unknown>,
202202
wrapValue: (value: any) => unknown,
203203
) {
204204
// note that taking ARRAY_ITERATE dependency here is not strictly equivalent
@@ -210,11 +210,13 @@ function iterator(
210210
// given that JS iterator can only be read once, this doesn't seem like
211211
// a plausible use-case, so this tracking simplification seems ok.
212212
const arr = shallowReadArray(self)
213-
const iter = (arr[method] as any)()
213+
const iter = (arr[method] as any)() as IterableIterator<unknown> & {
214+
_next: IterableIterator<unknown>['next']
215+
}
214216
if (arr !== self && !isShallow(self)) {
215-
;(iter as any)._next = iter.next
217+
iter._next = iter.next
216218
iter.next = () => {
217-
const result = (iter as any)._next()
219+
const result = iter._next()
218220
if (result.value) {
219221
result.value = wrapValue(result.value)
220222
}

packages/reactivity/src/baseHandlers.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const builtInSymbols = new Set(
3434
// but accessing them on Symbol leads to TypeError because Symbol is a strict mode
3535
// function
3636
.filter(key => key !== 'arguments' && key !== 'caller')
37-
.map(key => (Symbol as any)[key])
37+
.map(key => Symbol[key as keyof SymbolConstructor])
3838
.filter(isSymbol),
3939
)
4040

@@ -137,12 +137,12 @@ class MutableReactiveHandler extends BaseReactiveHandler {
137137
}
138138

139139
set(
140-
target: object,
140+
target: Record<string | symbol, unknown>,
141141
key: string | symbol,
142142
value: unknown,
143143
receiver: object,
144144
): boolean {
145-
let oldValue = (target as any)[key]
145+
let oldValue = target[key]
146146
if (!this._isShallow) {
147147
const isOldValueReadonly = isReadonly(oldValue)
148148
if (!isShallow(value) && !isReadonly(value)) {
@@ -177,24 +177,28 @@ class MutableReactiveHandler extends BaseReactiveHandler {
177177
return result
178178
}
179179

180-
deleteProperty(target: object, key: string | symbol): boolean {
180+
deleteProperty(
181+
target: Record<string | symbol, unknown>,
182+
key: string | symbol,
183+
): boolean {
181184
const hadKey = hasOwn(target, key)
182-
const oldValue = (target as any)[key]
185+
const oldValue = target[key]
183186
const result = Reflect.deleteProperty(target, key)
184187
if (result && hadKey) {
185188
trigger(target, TriggerOpTypes.DELETE, key, undefined, oldValue)
186189
}
187190
return result
188191
}
189192

190-
has(target: object, key: string | symbol): boolean {
193+
has(target: Record<string | symbol, unknown>, key: string | symbol): boolean {
191194
const result = Reflect.has(target, key)
192195
if (!isSymbol(key) || !builtInSymbols.has(key)) {
193196
track(target, TrackOpTypes.HAS, key)
194197
}
195198
return result
196199
}
197-
ownKeys(target: object): (string | symbol)[] {
200+
201+
ownKeys(target: Record<string | symbol, unknown>): (string | symbol)[] {
198202
track(
199203
target,
200204
TrackOpTypes.ITERATE,

packages/reactivity/src/collectionHandlers.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { toRaw, toReactive, toReadonly } from './reactive'
1+
import { type Target, toRaw, toReactive, toReadonly } from './reactive'
22
import { ITERATE_KEY, MAP_KEY_ITERATE_KEY, track, trigger } from './dep'
33
import { ReactiveFlags, TrackOpTypes, TriggerOpTypes } from './constants'
44
import { capitalize, hasChanged, hasOwn, isMap, toRawType } from '@vue/shared'
55
import { warn } from './warning'
66

77
type CollectionTypes = IterableCollections | WeakCollections
88

9-
type IterableCollections = Map<any, any> | Set<any>
10-
type WeakCollections = WeakMap<any, any> | WeakSet<any>
11-
type MapTypes = Map<any, any> | WeakMap<any, any>
12-
type SetTypes = Set<any> | WeakSet<any>
9+
type IterableCollections = (Map<any, any> | Set<any>) & Target
10+
type WeakCollections = (WeakMap<any, any> | WeakSet<any>) & Target
11+
type MapTypes = (Map<any, any> | WeakMap<any, any>) & Target
12+
type SetTypes = (Set<any> | WeakSet<any>) & Target
1313

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

@@ -24,7 +24,7 @@ function get(
2424
) {
2525
// #1772: readonly(reactive(Map)) should return readonly + reactive version
2626
// of the value
27-
target = (target as any)[ReactiveFlags.RAW]
27+
target = target[ReactiveFlags.RAW]
2828
const rawTarget = toRaw(target)
2929
const rawKey = toRaw(key)
3030
if (!isReadonly) {
@@ -47,7 +47,7 @@ function get(
4747
}
4848

4949
function has(this: CollectionTypes, key: unknown, isReadonly = false): boolean {
50-
const target = (this as any)[ReactiveFlags.RAW]
50+
const target = this[ReactiveFlags.RAW]
5151
const rawTarget = toRaw(target)
5252
const rawKey = toRaw(key)
5353
if (!isReadonly) {
@@ -62,7 +62,7 @@ function has(this: CollectionTypes, key: unknown, isReadonly = false): boolean {
6262
}
6363

6464
function size(target: IterableCollections, isReadonly = false) {
65-
target = (target as any)[ReactiveFlags.RAW]
65+
target = target[ReactiveFlags.RAW]
6666
!isReadonly && track(toRaw(target), TrackOpTypes.ITERATE, ITERATE_KEY)
6767
return Reflect.get(target, 'size', target)
6868
}
@@ -144,7 +144,7 @@ function createForEach(isReadonly: boolean, isShallow: boolean) {
144144
callback: Function,
145145
thisArg?: unknown,
146146
) {
147-
const observed = this as any
147+
const observed = this
148148
const target = observed[ReactiveFlags.RAW]
149149
const rawTarget = toRaw(target)
150150
const wrap = isShallow ? toShallow : isReadonly ? toReadonly : toReactive
@@ -180,7 +180,7 @@ function createIterableMethod(
180180
this: IterableCollections,
181181
...args: unknown[]
182182
): Iterable & Iterator {
183-
const target = (this as any)[ReactiveFlags.RAW]
183+
const target = this[ReactiveFlags.RAW]
184184
const rawTarget = toRaw(target)
185185
const targetIsMap = isMap(rawTarget)
186186
const isPair =

0 commit comments

Comments
 (0)