From f54be6a462cf863e3752ec7a12182e7d5aa14a96 Mon Sep 17 00:00:00 2001 From: Pick Date: Fri, 26 Jun 2020 00:04:14 +0800 Subject: [PATCH] refactor(types): simplify UnwrapRef + specify iterable method return type (#1444) --- packages/reactivity/src/collectionHandlers.ts | 18 +++++++++++++++++- packages/reactivity/src/ref.ts | 3 ++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/reactivity/src/collectionHandlers.ts b/packages/reactivity/src/collectionHandlers.ts index b9b45c78..1e908d96 100644 --- a/packages/reactivity/src/collectionHandlers.ts +++ b/packages/reactivity/src/collectionHandlers.ts @@ -154,12 +154,28 @@ function createForEach(isReadonly: boolean, shallow: boolean) { } } +interface Iterable { + [Symbol.iterator](): Iterator +} + +interface Iterator { + next(value?: any): IterationResult +} + +interface IterationResult { + value: any + done: boolean +} + function createIterableMethod( method: string | symbol, isReadonly: boolean, shallow: boolean ) { - return function(this: IterableCollections, ...args: unknown[]) { + return function( + this: IterableCollections, + ...args: unknown[] + ): Iterable & Iterator { const target = toRaw(this) const isMap = target instanceof Map const isPair = method === 'entries' || (method === Symbol.iterator && isMap) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index 285a9937..fe2b91c9 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -165,9 +165,10 @@ type UnwrapRefSimple = T extends | CollectionTypes | BaseTypes | Ref + | Array | RefUnwrapBailTypes[keyof RefUnwrapBailTypes] ? T - : T extends Array ? T : T extends object ? UnwrappedObject : T + : T extends object ? UnwrappedObject : T // Extract all known symbols from an object // when unwrapping Object the symbols are not `in keyof`, this should cover all the