]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
chore: simplifying and improve tupple & array output type on ref
authorpikax <david-181@hotmail.com>
Sat, 4 Jan 2020 14:09:52 +0000 (14:09 +0000)
committerpikax <david-181@hotmail.com>
Sat, 4 Jan 2020 14:09:52 +0000 (14:09 +0000)
packages/reactivity/src/ref.ts

index 3c3159ce30b907ddf2f7ffc2eaf0e17dcac4d7ca..b6df32a291f6ca94828a64b7d4212204c147d383 100644 (file)
@@ -82,22 +82,27 @@ function toProxyRef<T extends object, K extends keyof T>(
   } as any
 }
 
-type UnwrapArray<T> = { [P in keyof T]: UnwrapRef<T[P]> }
+// Super simple tuple checker
+type Tupple<T extends Array<any>> = T[0] extends T[1]
+  ? T[1] extends T[2] ? never : true
+  : true
 
-type UnwrapProp<T> = T extends ComputedRef<infer V>
-  ? UnwrapRef<V>
-  : T extends Ref<infer V>
-    ? UnwrapRef<V>
-    : T extends Function | CollectionTypes
-      ? T
-      : T extends object
-        ? UnwrapObject<T>
-        : T extends Array<infer V> ? Array<UnwrapRef<V>> & UnwrapArray<T> : T
+export type UnwrapRef<T> = T extends ComputedRef<infer V>
+  ? UnwrapRefSimple<V>
+  : T extends Ref<infer V> ? UnwrapRefSimple<V> : UnwrapRefSimple<T>
 
-type UnwrapObject<T> = { [K in keyof T]: UnwrapProp<T[K]> }
+type UnwrapRefSimple<T> = T extends Function | CollectionTypes
+  ? T
+  : T extends Array<infer V>
+    ? Tupple<T> extends never ? UnwrappedArray<V> : UnwrapTupple<T>
+    : T extends object ? UnwrappedObject<T> : T
 
-export type UnwrapRef<T> = T extends object
-  ? UnwrapObject<T>
-  : T extends Function | CollectionTypes
-    ? T
-    : T extends Array<infer V> ? Array<UnwrapRef<V>> & UnwrapArray<T> : T
+export type UnwrapTupple<T> = { [P in keyof T]: UnwrapRef<T[P]> } & {
+  length: number
+  [Symbol.iterator]: any
+  [Symbol.unscopables]: any
+}
+
+interface UnwrappedArray<T> extends Array<UnwrapRef<T>> {}
+
+type UnwrappedObject<T> = { [P in keyof T]: UnwrapRef<T[P]> }