From fbef1e18a1085e7f94561e437e2dc47e2f86a2f4 Mon Sep 17 00:00:00 2001 From: Hyden Date: Sun, 5 Sep 2021 19:29:18 +0800 Subject: [PATCH 1/2] Update Object.assign to support unlimited params Add Tuple2Intersection type, it support convert tuple to intersection. So, Object.assign can use it to define return type better. --- lib/lib.es2015.core.d.ts | 47 +++++++++++++++------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/lib/lib.es2015.core.d.ts b/lib/lib.es2015.core.d.ts index 79901dfe96b56..c34de0196f500 100644 --- a/lib/lib.es2015.core.d.ts +++ b/lib/lib.es2015.core.d.ts @@ -276,42 +276,31 @@ interface NumberConstructor { parseInt(string: string, radix?: number): number; } -interface ObjectConstructor { - /** - * Copy the values of all of the enumerable own properties from one or more source objects to a - * target object. Returns the target object. - * @param target The target object to copy to. - * @param source The source object from which to copy properties. - */ - assign(target: T, source: U): T & U; - - /** - * Copy the values of all of the enumerable own properties from one or more source objects to a - * target object. Returns the target object. - * @param target The target object to copy to. - * @param source1 The first source object from which to copy properties. - * @param source2 The second source object from which to copy properties. - */ - assign(target: T, source1: U, source2: V): T & U & V; - - /** - * Copy the values of all of the enumerable own properties from one or more source objects to a - * target object. Returns the target object. - * @param target The target object to copy to. - * @param source1 The first source object from which to copy properties. - * @param source2 The second source object from which to copy properties. - * @param source3 The third source object from which to copy properties. - */ - assign(target: T, source1: U, source2: V, source3: W): T & U & V & W; +/** +* Return a empty object type if the param's type is undefined +*/ +type Undefined2Object = T extends undefined ? {} : T; + +/** +* Remove the first tow item of tuple +*/ +type RemoveFirstTwo = T extends [a: First, b: Second, ...rest: infer P] ? P : []; + +/** +* Convert tuple to intersection with tuple item's type +*/ +type Tuple2Intersection = T[2] extends undefined + ? Undefined2Object & Undefined2Object + : Tuple2Intersection<[T[0], T[1]]> & Tuple2Intersection>; +interface ObjectConstructor { /** * Copy the values of all of the enumerable own properties from one or more source objects to a * target object. Returns the target object. * @param target The target object to copy to. * @param sources One or more source objects from which to copy properties */ - assign(target: object, ...sources: any[]): any; - + assign(target: T, ...sources: R): Tuple2Intersection<[T, ...R]>; /** * Returns an array of all symbol properties found directly on object o. * @param o Object to retrieve the symbols from. From a02422ed2a9300f7005c37c71c223af4c7e08ee2 Mon Sep 17 00:00:00 2001 From: Hyden Date: Mon, 6 Sep 2021 11:11:52 +0800 Subject: [PATCH 2/2] modify the spelling --- lib/lib.es2015.core.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/lib.es2015.core.d.ts b/lib/lib.es2015.core.d.ts index c34de0196f500..fa13765fb85ab 100644 --- a/lib/lib.es2015.core.d.ts +++ b/lib/lib.es2015.core.d.ts @@ -282,16 +282,16 @@ interface NumberConstructor { type Undefined2Object = T extends undefined ? {} : T; /** -* Remove the first tow item of tuple +* Remove the first two item of tuple */ type RemoveFirstTwo = T extends [a: First, b: Second, ...rest: infer P] ? P : []; /** * Convert tuple to intersection with tuple item's type */ -type Tuple2Intersection = T[2] extends undefined +type MergeTuple = T[2] extends undefined ? Undefined2Object & Undefined2Object - : Tuple2Intersection<[T[0], T[1]]> & Tuple2Intersection>; + : MergeTuple<[T[0], T[1]]> & MergeTuple>; interface ObjectConstructor { /** @@ -300,7 +300,7 @@ interface ObjectConstructor { * @param target The target object to copy to. * @param sources One or more source objects from which to copy properties */ - assign(target: T, ...sources: R): Tuple2Intersection<[T, ...R]>; + assign(target: T, ...sources: R): MergeTuple<[T, ...R]>; /** * Returns an array of all symbol properties found directly on object o. * @param o Object to retrieve the symbols from.