-
Notifications
You must be signed in to change notification settings - Fork 12.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ES2019 Object.fromEntries function #30934
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67e9d1f
add ES2019 Object.fromEntries function
saschanaz 32caaf6
add some comments
saschanaz 9bc4d6a
apply suggested changes
saschanaz 42626bd
add readonly and general any
saschanaz 7a76082
Merge branch 'master' into es2019-object
saschanaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/// <reference lib="es2018" /> | ||
/// <reference lib="es2019.array" /> | ||
/// <reference lib="es2019.object" /> | ||
/// <reference lib="es2019.string" /> | ||
/// <reference lib="es2019.symbol" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/// <reference lib="es2015.iterable" /> | ||
|
||
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<T = any>(entries: Iterable<[PropertyKey, T]>): { [k in PropertyKey]: T }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//// [objectFromEntries.ts] | ||
const o = Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]); | ||
const o2 = Object.fromEntries(new URLSearchParams()); | ||
const o3 = Object.fromEntries(new Map([[Symbol("key"), "value"]])); | ||
|
||
|
||
//// [objectFromEntries.js] | ||
const o = Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]); | ||
const o2 = Object.fromEntries(new URLSearchParams()); | ||
const o3 = Object.fromEntries(new Map([[Symbol("key"), "value"]])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
=== tests/cases/compiler/objectFromEntries.ts === | ||
const o = Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]); | ||
>o : Symbol(o, Decl(objectFromEntries.ts, 0, 5)) | ||
>Object.fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --)) | ||
|
||
const o2 = Object.fromEntries(new URLSearchParams()); | ||
>o2 : Symbol(o2, Decl(objectFromEntries.ts, 1, 5)) | ||
>Object.fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --)) | ||
>URLSearchParams : Symbol(URLSearchParams, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --), Decl(lib.dom.iterable.d.ts, --, --)) | ||
|
||
const o3 = Object.fromEntries(new Map([[Symbol("key"), "value"]])); | ||
>o3 : Symbol(o3, Decl(objectFromEntries.ts, 2, 5)) | ||
>Object.fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
>fromEntries : Symbol(ObjectConstructor.fromEntries, Decl(lib.es2019.object.d.ts, --, --)) | ||
>Map : Symbol(Map, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --)) | ||
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
=== tests/cases/compiler/objectFromEntries.ts === | ||
const o = Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]); | ||
>o : { [x: string]: number; [x: number]: number; } | ||
>Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]) : { [x: string]: number; [x: number]: number; } | ||
>Object.fromEntries : <T = any>(entries: Iterable<[string | number | symbol, T]>) => { [x: string]: T; [x: number]: T; } | ||
>Object : ObjectConstructor | ||
>fromEntries : <T = any>(entries: Iterable<[string | number | symbol, T]>) => { [x: string]: T; [x: number]: T; } | ||
>[['a', 1], ['b', 2], ['c', 3]] : [string, number][] | ||
>['a', 1] : [string, number] | ||
>'a' : "a" | ||
>1 : 1 | ||
>['b', 2] : [string, number] | ||
>'b' : "b" | ||
>2 : 2 | ||
>['c', 3] : [string, number] | ||
>'c' : "c" | ||
>3 : 3 | ||
|
||
const o2 = Object.fromEntries(new URLSearchParams()); | ||
>o2 : { [x: string]: string; [x: number]: string; } | ||
>Object.fromEntries(new URLSearchParams()) : { [x: string]: string; [x: number]: string; } | ||
>Object.fromEntries : <T = any>(entries: Iterable<[string | number | symbol, T]>) => { [x: string]: T; [x: number]: T; } | ||
>Object : ObjectConstructor | ||
>fromEntries : <T = any>(entries: Iterable<[string | number | symbol, T]>) => { [x: string]: T; [x: number]: T; } | ||
>new URLSearchParams() : URLSearchParams | ||
>URLSearchParams : { new (init?: string | URLSearchParams | string[][] | Record<string, string>): URLSearchParams; prototype: URLSearchParams; } | ||
|
||
const o3 = Object.fromEntries(new Map([[Symbol("key"), "value"]])); | ||
>o3 : { [x: string]: string; [x: number]: string; } | ||
>Object.fromEntries(new Map([[Symbol("key"), "value"]])) : { [x: string]: string; [x: number]: string; } | ||
>Object.fromEntries : <T = any>(entries: Iterable<[string | number | symbol, T]>) => { [x: string]: T; [x: number]: T; } | ||
>Object : ObjectConstructor | ||
>fromEntries : <T = any>(entries: Iterable<[string | number | symbol, T]>) => { [x: string]: T; [x: number]: T; } | ||
>new Map([[Symbol("key"), "value"]]) : Map<symbol, string> | ||
>Map : MapConstructor | ||
>[[Symbol("key"), "value"]] : [symbol, string][] | ||
>[Symbol("key"), "value"] : [symbol, string] | ||
>Symbol("key") : symbol | ||
>Symbol : SymbolConstructor | ||
>"key" : "key" | ||
>"value" : "value" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// @target: es2019 | ||
|
||
const o = Object.fromEntries([['a', 1], ['b', 2], ['c', 3]]); | ||
const o2 = Object.fromEntries(new URLSearchParams()); | ||
const o3 = Object.fromEntries(new Map([[Symbol("key"), "value"]])); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #29721