From c6b7afcc23faefd8c504c3c5705ecb5b0f4be0fd Mon Sep 17 00:00:00 2001 From: Andrew / Kana Date: Mon, 7 Oct 2019 03:11:52 +0300 Subject: [PATCH] types: use recursion for unwrapping refs (#137) --- packages/reactivity/src/ref.ts | 87 +++++----------------------------- 1 file changed, 11 insertions(+), 76 deletions(-) diff --git a/packages/reactivity/src/ref.ts b/packages/reactivity/src/ref.ts index b0de0586..113696bd 100644 --- a/packages/reactivity/src/ref.ts +++ b/packages/reactivity/src/ref.ts @@ -68,80 +68,15 @@ type BailTypes = | WeakSet // Recursively unwraps nested value bindings. -// Unfortunately TS cannot do recursive types, but this should be enough for -// practical use cases... -export type UnwrapRef = T extends Ref - ? UnwrapRef2 - : T extends Array - ? Array> +export type UnwrapRef = { + ref: T extends Ref ? UnwrapRef : T + array: T extends Array ? Array> : T + object: { [K in keyof T]: UnwrapRef } + stop: T +}[T extends Ref + ? 'ref' + : T extends Array + ? 'array' : T extends BailTypes - ? T // bail out on types that shouldn't be unwrapped - : T extends object ? { [K in keyof T]: UnwrapRef2 } : T - -type UnwrapRef2 = T extends Ref - ? UnwrapRef3 - : T extends Array - ? Array> - : T extends BailTypes - ? T - : T extends object ? { [K in keyof T]: UnwrapRef3 } : T - -type UnwrapRef3 = T extends Ref - ? UnwrapRef4 - : T extends Array - ? Array> - : T extends BailTypes - ? T - : T extends object ? { [K in keyof T]: UnwrapRef4 } : T - -type UnwrapRef4 = T extends Ref - ? UnwrapRef5 - : T extends Array - ? Array> - : T extends BailTypes - ? T - : T extends object ? { [K in keyof T]: UnwrapRef5 } : T - -type UnwrapRef5 = T extends Ref - ? UnwrapRef6 - : T extends Array - ? Array> - : T extends BailTypes - ? T - : T extends object ? { [K in keyof T]: UnwrapRef6 } : T - -type UnwrapRef6 = T extends Ref - ? UnwrapRef7 - : T extends Array - ? Array> - : T extends BailTypes - ? T - : T extends object ? { [K in keyof T]: UnwrapRef7 } : T - -type UnwrapRef7 = T extends Ref - ? UnwrapRef8 - : T extends Array - ? Array> - : T extends BailTypes - ? T - : T extends object ? { [K in keyof T]: UnwrapRef8 } : T - -type UnwrapRef8 = T extends Ref - ? UnwrapRef9 - : T extends Array - ? Array> - : T extends BailTypes - ? T - : T extends object ? { [K in keyof T]: UnwrapRef9 } : T - -type UnwrapRef9 = T extends Ref - ? UnwrapRef10 - : T extends Array - ? Array> - : T extends BailTypes - ? T - : T extends object ? { [K in keyof T]: UnwrapRef10 } : T - -type UnwrapRef10 = T extends Ref - ? V // stop recursion - : T + ? 'stop' // bail out on types that shouldn't be unwrapped + : T extends object ? 'object' : 'stop']