Skip to content

Commit b588618

Browse files
committed
types: massive refactor
1 parent 522beaa commit b588618

21 files changed

+308
-258
lines changed

packages/reactivity/src/baseHandlers.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const builtInSymbols = new Set(
1212
)
1313

1414
function createGetter(isReadonly: boolean) {
15-
return function get(target: any, key: string | symbol, receiver: any) {
15+
return function get(target: object, key: string | symbol, receiver: object) {
1616
const res = Reflect.get(target, key, receiver)
1717
if (isSymbol(key) && builtInSymbols.has(key)) {
1818
return res
@@ -32,13 +32,13 @@ function createGetter(isReadonly: boolean) {
3232
}
3333

3434
function set(
35-
target: any,
35+
target: object,
3636
key: string | symbol,
37-
value: any,
38-
receiver: any
37+
value: unknown,
38+
receiver: object
3939
): boolean {
4040
value = toRaw(value)
41-
const oldValue = target[key]
41+
const oldValue = (target as any)[key]
4242
if (isRef(oldValue) && !isRef(value)) {
4343
oldValue.value = value
4444
return true
@@ -66,9 +66,9 @@ function set(
6666
return result
6767
}
6868

69-
function deleteProperty(target: any, key: string | symbol): boolean {
69+
function deleteProperty(target: object, key: string | symbol): boolean {
7070
const hadKey = hasOwn(target, key)
71-
const oldValue = target[key]
71+
const oldValue = (target as any)[key]
7272
const result = Reflect.deleteProperty(target, key)
7373
if (result && hadKey) {
7474
/* istanbul ignore else */
@@ -81,29 +81,34 @@ function deleteProperty(target: any, key: string | symbol): boolean {
8181
return result
8282
}
8383

84-
function has(target: any, key: string | symbol): boolean {
84+
function has(target: object, key: string | symbol): boolean {
8585
const result = Reflect.has(target, key)
8686
track(target, OperationTypes.HAS, key)
8787
return result
8888
}
8989

90-
function ownKeys(target: any): (string | number | symbol)[] {
90+
function ownKeys(target: object): (string | number | symbol)[] {
9191
track(target, OperationTypes.ITERATE)
9292
return Reflect.ownKeys(target)
9393
}
9494

95-
export const mutableHandlers: ProxyHandler<any> = {
95+
export const mutableHandlers: ProxyHandler<object> = {
9696
get: createGetter(false),
9797
set,
9898
deleteProperty,
9999
has,
100100
ownKeys
101101
}
102102

103-
export const readonlyHandlers: ProxyHandler<any> = {
103+
export const readonlyHandlers: ProxyHandler<object> = {
104104
get: createGetter(true),
105105

106-
set(target: any, key: string | symbol, value: any, receiver: any): boolean {
106+
set(
107+
target: object,
108+
key: string | symbol,
109+
value: unknown,
110+
receiver: object
111+
): boolean {
107112
if (LOCKED) {
108113
if (__DEV__) {
109114
console.warn(
@@ -117,7 +122,7 @@ export const readonlyHandlers: ProxyHandler<any> = {
117122
}
118123
},
119124

120-
deleteProperty(target: any, key: string | symbol): boolean {
125+
deleteProperty(target: object, key: string | symbol): boolean {
121126
if (LOCKED) {
122127
if (__DEV__) {
123128
console.warn(

packages/reactivity/src/collectionHandlers.ts

Lines changed: 79 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,67 @@ import { OperationTypes } from './operations'
44
import { LOCKED } from './lock'
55
import { isObject, capitalize, hasOwn } from '@vue/shared'
66

7-
const toReactive = (value: any) => (isObject(value) ? reactive(value) : value)
8-
const toReadonly = (value: any) => (isObject(value) ? readonly(value) : value)
7+
export type CollectionTypes = IterableCollections | WeakCollections
98

10-
function get(target: any, key: any, wrap: (t: any) => any): any {
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>
13+
14+
const toReactive = <T extends unknown>(value: T): T =>
15+
isObject(value) ? reactive(value) : value
16+
17+
const toReadonly = <T extends unknown>(value: T): T =>
18+
isObject(value) ? readonly(value) : value
19+
20+
const getProto = <T extends CollectionTypes>(v: T): any =>
21+
Reflect.getPrototypeOf(v)
22+
23+
function get(
24+
target: MapTypes,
25+
key: unknown,
26+
wrap: typeof toReactive | typeof toReadonly
27+
) {
1128
target = toRaw(target)
1229
key = toRaw(key)
13-
const proto: any = Reflect.getPrototypeOf(target)
1430
track(target, OperationTypes.GET, key)
15-
const res = proto.get.call(target, key)
16-
return wrap(res)
31+
return wrap(getProto(target).get.call(target, key))
1732
}
1833

19-
function has(this: any, key: any): boolean {
34+
function has(this: CollectionTypes, key: unknown): boolean {
2035
const target = toRaw(this)
2136
key = toRaw(key)
22-
const proto: any = Reflect.getPrototypeOf(target)
2337
track(target, OperationTypes.HAS, key)
24-
return proto.has.call(target, key)
38+
return getProto(target).has.call(target, key)
2539
}
2640

27-
function size(target: any) {
41+
function size(target: IterableCollections) {
2842
target = toRaw(target)
29-
const proto = Reflect.getPrototypeOf(target)
3043
track(target, OperationTypes.ITERATE)
31-
return Reflect.get(proto, 'size', target)
44+
return Reflect.get(getProto(target), 'size', target)
3245
}
3346

34-
function add(this: any, value: any) {
47+
function add(this: SetTypes, value: unknown) {
3548
value = toRaw(value)
3649
const target = toRaw(this)
37-
const proto: any = Reflect.getPrototypeOf(this)
50+
const proto = getProto(target)
3851
const hadKey = proto.has.call(target, value)
3952
const result = proto.add.call(target, value)
4053
if (!hadKey) {
4154
/* istanbul ignore else */
4255
if (__DEV__) {
43-
trigger(target, OperationTypes.ADD, value, { value })
56+
trigger(target, OperationTypes.ADD, value, { newValue: value })
4457
} else {
4558
trigger(target, OperationTypes.ADD, value)
4659
}
4760
}
4861
return result
4962
}
5063

51-
function set(this: any, key: any, value: any) {
64+
function set(this: MapTypes, key: unknown, value: unknown) {
5265
value = toRaw(value)
5366
const target = toRaw(this)
54-
const proto: any = Reflect.getPrototypeOf(this)
67+
const proto = getProto(target)
5568
const hadKey = proto.has.call(target, key)
5669
const oldValue = proto.get.call(target, key)
5770
const result = proto.set.call(target, key, value)
@@ -75,9 +88,9 @@ function set(this: any, key: any, value: any) {
7588
return result
7689
}
7790

78-
function deleteEntry(this: any, key: any) {
91+
function deleteEntry(this: CollectionTypes, key: unknown) {
7992
const target = toRaw(this)
80-
const proto: any = Reflect.getPrototypeOf(this)
93+
const proto = getProto(target)
8194
const hadKey = proto.has.call(target, key)
8295
const oldValue = proto.get ? proto.get.call(target, key) : undefined
8396
// forward the operation before queueing reactions
@@ -93,13 +106,16 @@ function deleteEntry(this: any, key: any) {
93106
return result
94107
}
95108

96-
function clear(this: any) {
109+
function clear(this: IterableCollections) {
97110
const target = toRaw(this)
98-
const proto: any = Reflect.getPrototypeOf(this)
99111
const hadItems = target.size !== 0
100-
const oldTarget = target instanceof Map ? new Map(target) : new Set(target)
112+
const oldTarget = __DEV__
113+
? target instanceof Map
114+
? new Map(target)
115+
: new Set(target)
116+
: undefined
101117
// forward the operation before queueing reactions
102-
const result = proto.clear.call(target)
118+
const result = getProto(target).clear.call(target)
103119
if (hadItems) {
104120
/* istanbul ignore else */
105121
if (__DEV__) {
@@ -112,30 +128,32 @@ function clear(this: any) {
112128
}
113129

114130
function createForEach(isReadonly: boolean) {
115-
return function forEach(this: any, callback: Function, thisArg?: any) {
131+
return function forEach(
132+
this: IterableCollections,
133+
callback: Function,
134+
thisArg?: unknown
135+
) {
116136
const observed = this
117137
const target = toRaw(observed)
118-
const proto: any = Reflect.getPrototypeOf(target)
119138
const wrap = isReadonly ? toReadonly : toReactive
120139
track(target, OperationTypes.ITERATE)
121140
// important: create sure the callback is
122141
// 1. invoked with the reactive map as `this` and 3rd arg
123142
// 2. the value received should be a corresponding reactive/readonly.
124-
function wrappedCallback(value: any, key: any) {
143+
function wrappedCallback(value: unknown, key: unknown) {
125144
return callback.call(observed, wrap(value), wrap(key), observed)
126145
}
127-
return proto.forEach.call(target, wrappedCallback, thisArg)
146+
return getProto(target).forEach.call(target, wrappedCallback, thisArg)
128147
}
129148
}
130149

131150
function createIterableMethod(method: string | symbol, isReadonly: boolean) {
132-
return function(this: any, ...args: any[]) {
151+
return function(this: IterableCollections, ...args: unknown[]) {
133152
const target = toRaw(this)
134-
const proto: any = Reflect.getPrototypeOf(target)
135153
const isPair =
136154
method === 'entries' ||
137155
(method === Symbol.iterator && target instanceof Map)
138-
const innerIterator = proto[method].apply(target, args)
156+
const innerIterator = getProto(target)[method].apply(target, args)
139157
const wrap = isReadonly ? toReadonly : toReactive
140158
track(target, OperationTypes.ITERATE)
141159
// return a wrapped iterator which returns observed versions of the
@@ -163,7 +181,7 @@ function createReadonlyMethod(
163181
method: Function,
164182
type: OperationTypes
165183
): Function {
166-
return function(this: any, ...args: any[]) {
184+
return function(this: CollectionTypes, ...args: unknown[]) {
167185
if (LOCKED) {
168186
if (__DEV__) {
169187
const key = args[0] ? `on key "${args[0]}" ` : ``
@@ -179,11 +197,11 @@ function createReadonlyMethod(
179197
}
180198
}
181199

182-
const mutableInstrumentations: any = {
183-
get(key: any) {
200+
const mutableInstrumentations: Record<string, Function> = {
201+
get(this: MapTypes, key: unknown) {
184202
return get(this, key, toReactive)
185203
},
186-
get size() {
204+
get size(this: IterableCollections) {
187205
return size(this)
188206
},
189207
has,
@@ -194,11 +212,11 @@ const mutableInstrumentations: any = {
194212
forEach: createForEach(false)
195213
}
196214

197-
const readonlyInstrumentations: any = {
198-
get(key: any) {
215+
const readonlyInstrumentations: Record<string, Function> = {
216+
get(this: MapTypes, key: unknown) {
199217
return get(this, key, toReadonly)
200218
},
201-
get size() {
219+
get size(this: IterableCollections) {
202220
return size(this)
203221
},
204222
has,
@@ -211,26 +229,37 @@ const readonlyInstrumentations: any = {
211229

212230
const iteratorMethods = ['keys', 'values', 'entries', Symbol.iterator]
213231
iteratorMethods.forEach(method => {
214-
mutableInstrumentations[method] = createIterableMethod(method, false)
215-
readonlyInstrumentations[method] = createIterableMethod(method, true)
232+
mutableInstrumentations[method as string] = createIterableMethod(
233+
method,
234+
false
235+
)
236+
readonlyInstrumentations[method as string] = createIterableMethod(
237+
method,
238+
true
239+
)
216240
})
217241

218-
function createInstrumentationGetter(instrumentations: any) {
219-
return function getInstrumented(
220-
target: any,
242+
function createInstrumentationGetter(
243+
instrumentations: Record<string, Function>
244+
) {
245+
return (
246+
target: CollectionTypes,
221247
key: string | symbol,
222-
receiver: any
223-
) {
224-
target =
225-
hasOwn(instrumentations, key) && key in target ? instrumentations : target
226-
return Reflect.get(target, key, receiver)
227-
}
248+
receiver: CollectionTypes
249+
) =>
250+
Reflect.get(
251+
hasOwn(instrumentations, key) && key in target
252+
? instrumentations
253+
: target,
254+
key,
255+
receiver
256+
)
228257
}
229258

230-
export const mutableCollectionHandlers: ProxyHandler<any> = {
259+
export const mutableCollectionHandlers: ProxyHandler<CollectionTypes> = {
231260
get: createInstrumentationGetter(mutableInstrumentations)
232261
}
233262

234-
export const readonlyCollectionHandlers: ProxyHandler<any> = {
263+
export const readonlyCollectionHandlers: ProxyHandler<CollectionTypes> = {
235264
get: createInstrumentationGetter(readonlyInstrumentations)
236265
}

packages/reactivity/src/computed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function computed<T>(
2424
): WritableComputedRef<T>
2525
export function computed<T>(
2626
getterOrOptions: ComputedGetter<T> | WritableComputedOptions<T>
27-
): any {
27+
) {
2828
const isReadonly = isFunction(getterOrOptions)
2929
const getter = isReadonly
3030
? (getterOrOptions as ComputedGetter<T>)

0 commit comments

Comments
 (0)