From 1f4fb016146606090b503ebe44877325a056a7ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Mon, 12 Jun 2023 23:01:36 +0800 Subject: [PATCH] chore: optimize TypeScript types --- packages/reactivity/src/collectionHandlers.ts | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/packages/reactivity/src/collectionHandlers.ts b/packages/reactivity/src/collectionHandlers.ts index 381bbad6c28..0b518dba647 100644 --- a/packages/reactivity/src/collectionHandlers.ts +++ b/packages/reactivity/src/collectionHandlers.ts @@ -227,8 +227,10 @@ function createReadonlyMethod(type: TriggerOpTypes): Function { } } +type Instrumentations = Record + function createInstrumentations() { - const mutableInstrumentations: Record = { + const mutableInstrumentations: Instrumentations = { get(this: MapTypes, key: unknown) { return get(this, key) }, @@ -243,7 +245,7 @@ function createInstrumentations() { forEach: createForEach(false, false) } - const shallowInstrumentations: Record = { + const shallowInstrumentations: Instrumentations = { get(this: MapTypes, key: unknown) { return get(this, key, false, true) }, @@ -258,7 +260,7 @@ function createInstrumentations() { forEach: createForEach(false, true) } - const readonlyInstrumentations: Record = { + const readonlyInstrumentations: Instrumentations = { get(this: MapTypes, key: unknown) { return get(this, key, true) }, @@ -275,7 +277,7 @@ function createInstrumentations() { forEach: createForEach(true, false) } - const shallowReadonlyInstrumentations: Record = { + const shallowReadonlyInstrumentations: Instrumentations = { get(this: MapTypes, key: unknown) { return get(this, key, true, true) }, @@ -292,24 +294,18 @@ function createInstrumentations() { forEach: createForEach(true, true) } - const iteratorMethods = ['keys', 'values', 'entries', Symbol.iterator] + const iteratorMethods = [ + 'keys', + 'values', + 'entries', + Symbol.iterator + ] as const + iteratorMethods.forEach(method => { - mutableInstrumentations[method as string] = createIterableMethod( - method, - false, - false - ) - readonlyInstrumentations[method as string] = createIterableMethod( - method, - true, - false - ) - shallowInstrumentations[method as string] = createIterableMethod( - method, - false, - true - ) - shallowReadonlyInstrumentations[method as string] = createIterableMethod( + mutableInstrumentations[method] = createIterableMethod(method, false, false) + readonlyInstrumentations[method] = createIterableMethod(method, true, false) + shallowInstrumentations[method] = createIterableMethod(method, false, true) + shallowReadonlyInstrumentations[method] = createIterableMethod( method, true, true