From 898810ab274fd60d2cc971fbc93e6eee290eb0a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bazyli=20Brz=C3=B3ska?= Date: Fri, 5 Aug 2022 20:27:18 -0700 Subject: [PATCH 1/4] Add stricter Object.fromEntries for constructing from const tuples `Object.fromEntries` as is declared today resulted in a loss of key names and a unionization of all values, or a complete loss of information and type of `any`. This change makes it so that it is possible to create strict objects from const tuples. ```ts const obj1 = Object.fromEntries([ ['1', 2], ['3', 4], ] as const) // now results in an object of type: {1: 2, 3: 4}, previously { [k: string]: 2 | 4 } const obj2 = Object.fromEntries([ ['1', 2], ['3', '4'], ] as const) // now results in an object of type: {1: 2, 3: '4'}, previously any ``` --- lib/lib.es2019.object.d.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/lib.es2019.object.d.ts b/lib/lib.es2019.object.d.ts index 09b937af044f8..69648dcdef1e2 100644 --- a/lib/lib.es2019.object.d.ts +++ b/lib/lib.es2019.object.d.ts @@ -21,6 +21,14 @@ and limitations under the License. /// interface ObjectConstructor { + /** + * Returns an object created by key-value entries for properties and methods + * @param entries An iterable object that contains key-value entries for properties and methods. + */ + ( + entries: Iterable, + ) => { [K in KeyValue[0]]: KeyValue extends readonly [K, infer V] ? V : never }; + /** * Returns an object created by key-value entries for properties and methods * @param entries An iterable object that contains key-value entries for properties and methods. From 7545c59de1c3df5ff2f1fbf12edd8528d50876a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bazyli=20Brz=C3=B3ska?= Date: Wed, 17 Aug 2022 21:19:57 -0700 Subject: [PATCH 2/4] Target only fixed length tuples Only create strict types from const tuples, since we cannot guarantee the existence of any key in an iterable. --- lib/lib.es2019.object.d.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/lib.es2019.object.d.ts b/lib/lib.es2019.object.d.ts index 69648dcdef1e2..0545e1fedd534 100644 --- a/lib/lib.es2019.object.d.ts +++ b/lib/lib.es2019.object.d.ts @@ -25,15 +25,11 @@ interface ObjectConstructor { * Returns an object created by key-value entries for properties and methods * @param entries An iterable object that contains key-value entries for properties and methods. */ - ( + ( entries: Iterable, - ) => { [K in KeyValue[0]]: KeyValue extends readonly [K, infer V] ? V : never }; - - /** - * Returns an object created by key-value entries for properties and methods - * @param entries An iterable object that contains key-value entries for properties and methods. - */ - fromEntries(entries: Iterable): { [k: string]: T }; + ) => [KeyValue] extends [[PropertyKey, any]] + ? { [k: string]: KeyValue[1] } + : { [K in KeyValue[0]]: KeyValue extends readonly [K, infer V] ? V : never }; /** * Returns an object created by key-value entries for properties and methods From bef1f02754701742e06fbe23694bbdb9a6a3b928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bazyli=20Brz=C3=B3ska?= Date: Thu, 18 Aug 2022 04:34:51 +0000 Subject: [PATCH 3/4] Move changes to the correct file --- lib/lib.es2019.object.d.ts | 6 +----- src/lib/es2019.object.d.ts | 6 +++++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/lib.es2019.object.d.ts b/lib/lib.es2019.object.d.ts index 0545e1fedd534..09b937af044f8 100644 --- a/lib/lib.es2019.object.d.ts +++ b/lib/lib.es2019.object.d.ts @@ -25,11 +25,7 @@ interface ObjectConstructor { * Returns an object created by key-value entries for properties and methods * @param entries An iterable object that contains key-value entries for properties and methods. */ - ( - entries: Iterable, - ) => [KeyValue] extends [[PropertyKey, any]] - ? { [k: string]: KeyValue[1] } - : { [K in KeyValue[0]]: KeyValue extends readonly [K, infer V] ? V : never }; + fromEntries(entries: Iterable): { [k: string]: T }; /** * Returns an object created by key-value entries for properties and methods diff --git a/src/lib/es2019.object.d.ts b/src/lib/es2019.object.d.ts index e3518b7b9d689..f9437d51dc5f5 100644 --- a/src/lib/es2019.object.d.ts +++ b/src/lib/es2019.object.d.ts @@ -5,7 +5,11 @@ interface ObjectConstructor { * Returns an object created by key-value entries for properties and methods * @param entries An iterable object that contains key-value entries for properties and methods. */ - fromEntries(entries: Iterable): { [k: string]: T }; + ( + entries: Iterable, + ): [KeyValue] extends [[PropertyKey, any]] + ? { [k: string]: KeyValue[1] } + : { [K in KeyValue[0]]: KeyValue extends readonly [K, infer V] ? V : never }; /** * Returns an object created by key-value entries for properties and methods From 5f38d950bb564d0a20085a077abb60fe6f153f9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bazyli=20Brz=C3=B3ska?= Date: Thu, 18 Aug 2022 04:35:49 +0000 Subject: [PATCH 4/4] Fix signature --- src/lib/es2019.object.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/es2019.object.d.ts b/src/lib/es2019.object.d.ts index f9437d51dc5f5..98e909bb4de50 100644 --- a/src/lib/es2019.object.d.ts +++ b/src/lib/es2019.object.d.ts @@ -5,7 +5,7 @@ interface ObjectConstructor { * Returns an object created by key-value entries for properties and methods * @param entries An iterable object that contains key-value entries for properties and methods. */ - ( + fromEntries( entries: Iterable, ): [KeyValue] extends [[PropertyKey, any]] ? { [k: string]: KeyValue[1] }