Skip to content
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

Object.keys() types refinement, and Object.entries() types bugfix #12253

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/es2017.object.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface ObjectConstructor {
* Returns an array of key/values of the enumerable properties of an object
* @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.
*/
entries<T>(o: Array<T>): [string, T][];
entries<T extends { [key: string]: any }, K extends keyof T>(o: T): [keyof T, T[K]][];
entries(o: any): [string, any][];
}
2 changes: 2 additions & 0 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ interface ObjectConstructor {
* Returns the names of the enumerable properties and methods of an object.
* @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object.
*/
keys<T>(o: Array<T>): string[];
keys<T extends { [key: string]: any }>(o: T): (keyof T)[];
keys(o: any): string[];
}

Expand Down
11 changes: 9 additions & 2 deletions tests/baselines/reference/useObjectValuesAndEntries1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ for (var x of Object.values(o)) {

var entries = Object.entries(o); // <-- entries: ['a' | 'b', number][]
var entries1 = Object.entries(1); // <-- entries: [string, any][]
var entries2 = Object.entries({a: true, b: 2}) // ['a' | 'b', number | boolean][]
var entries2 = Object.entries({a: true, b: 2}) // [('a' | 'b'), number | boolean][]
var entries3 = Object.entries({}) // [never, any][]
var entries4 = Object.entries([1, 2, 3, 4]); // [string, number][]

var x2: { [index: string]: any } = {1: 2};
var entries5 = Object.entries(x2); // [string, any][]


//// [useObjectValuesAndEntries1.js]
Expand All @@ -20,5 +24,8 @@ for (var _i = 0, _a = Object.values(o); _i < _a.length; _i++) {
}
var entries = Object.entries(o); // <-- entries: ['a' | 'b', number][]
var entries1 = Object.entries(1); // <-- entries: [string, any][]
var entries2 = Object.entries({ a: true, b: 2 }); // ['a' | 'b', number | boolean][]
var entries2 = Object.entries({ a: true, b: 2 }); // [('a' | 'b'), number | boolean][]
var entries3 = Object.entries({}); // [never, any][]
var entries4 = Object.entries([1, 2, 3, 4]); // [string, number][]
var x2 = { 1: 2 };
var entries5 = Object.entries(x2); // [string, any][]
35 changes: 26 additions & 9 deletions tests/baselines/reference/useObjectValuesAndEntries1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,45 @@ for (var x of Object.values(o)) {

var entries = Object.entries(o); // <-- entries: ['a' | 'b', number][]
>entries : Symbol(entries, Decl(useObjectValuesAndEntries1.ts, 7, 3))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>o : Symbol(o, Decl(useObjectValuesAndEntries1.ts, 1, 3))

var entries1 = Object.entries(1); // <-- entries: [string, any][]
>entries1 : Symbol(entries1, Decl(useObjectValuesAndEntries1.ts, 8, 3))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))

var entries2 = Object.entries({a: true, b: 2}) // ['a' | 'b', number | boolean][]
var entries2 = Object.entries({a: true, b: 2}) // [('a' | 'b'), number | boolean][]
>entries2 : Symbol(entries2, Decl(useObjectValuesAndEntries1.ts, 9, 3))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>a : Symbol(a, Decl(useObjectValuesAndEntries1.ts, 9, 31))
>b : Symbol(b, Decl(useObjectValuesAndEntries1.ts, 9, 39))

var entries3 = Object.entries({}) // [never, any][]
>entries3 : Symbol(entries3, Decl(useObjectValuesAndEntries1.ts, 10, 3))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))

var entries4 = Object.entries([1, 2, 3, 4]); // [string, number][]
>entries4 : Symbol(entries4, Decl(useObjectValuesAndEntries1.ts, 11, 3))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))

var x2: { [index: string]: any } = {1: 2};
>x2 : Symbol(x2, Decl(useObjectValuesAndEntries1.ts, 13, 3))
>index : Symbol(index, Decl(useObjectValuesAndEntries1.ts, 13, 11))

var entries5 = Object.entries(x2); // [string, any][]
>entries5 : Symbol(entries5, Decl(useObjectValuesAndEntries1.ts, 14, 3))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>x2 : Symbol(x2, Decl(useObjectValuesAndEntries1.ts, 13, 3))

44 changes: 35 additions & 9 deletions tests/baselines/reference/useObjectValuesAndEntries1.types
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ for (var x of Object.values(o)) {
var entries = Object.entries(o); // <-- entries: ['a' | 'b', number][]
>entries : ["a" | "b", number][]
>Object.entries(o) : ["a" | "b", number][]
>Object.entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object.entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>o : { a: number; b: number; }

var entries1 = Object.entries(1); // <-- entries: [string, any][]
>entries1 : [string, any][]
>Object.entries(1) : [string, any][]
>Object.entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object.entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>1 : 1

var entries2 = Object.entries({a: true, b: 2}) // ['a' | 'b', number | boolean][]
var entries2 = Object.entries({a: true, b: 2}) // [('a' | 'b'), number | boolean][]
>entries2 : ["a" | "b", number | boolean][]
>Object.entries({a: true, b: 2}) : ["a" | "b", number | boolean][]
>Object.entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object.entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>{a: true, b: 2} : { a: true; b: number; }
>a : boolean
>true : true
Expand All @@ -52,8 +52,34 @@ var entries2 = Object.entries({a: true, b: 2}) // ['a' | 'b', number | boolean][
var entries3 = Object.entries({}) // [never, any][]
>entries3 : [never, any][]
>Object.entries({}) : [never, any][]
>Object.entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object.entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>{} : {}

var entries4 = Object.entries([1, 2, 3, 4]); // [string, number][]
>entries4 : [string, number][]
>Object.entries([1, 2, 3, 4]) : [string, number][]
>Object.entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>[1, 2, 3, 4] : number[]
>1 : 1
>2 : 2
>3 : 3
>4 : 4

var x2: { [index: string]: any } = {1: 2};
>x2 : { [index: string]: any; }
>index : string
>{1: 2} : { 1: number; }
>2 : 2

var entries5 = Object.entries(x2); // [string, any][]
>entries5 : [string, any][]
>Object.entries(x2) : [string, any][]
>Object.entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>x2 : { [index: string]: any; }

4 changes: 2 additions & 2 deletions tests/baselines/reference/useObjectValuesAndEntries4.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ for (var x of Object.values(o)) {

var entries = Object.entries(o);
>entries : Symbol(entries, Decl(useObjectValuesAndEntries4.ts, 7, 3))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object.entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>entries : Symbol(ObjectConstructor.entries, Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --), Decl(lib.es2017.object.d.ts, --, --))
>o : Symbol(o, Decl(useObjectValuesAndEntries4.ts, 1, 3))

4 changes: 2 additions & 2 deletions tests/baselines/reference/useObjectValuesAndEntries4.types
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ for (var x of Object.values(o)) {
var entries = Object.entries(o);
>entries : ["a" | "b", number][]
>Object.entries(o) : ["a" | "b", number][]
>Object.entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object.entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>o : { a: number; b: number; }

6 changes: 5 additions & 1 deletion tests/cases/conformance/es2017/useObjectValuesAndEntries1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ for (var x of Object.values(o)) {

var entries = Object.entries(o); // <-- entries: ['a' | 'b', number][]
var entries1 = Object.entries(1); // <-- entries: [string, any][]
var entries2 = Object.entries({a: true, b: 2}) // ['a' | 'b', number | boolean][]
var entries2 = Object.entries({a: true, b: 2}) // [('a' | 'b'), number | boolean][]
var entries3 = Object.entries({}) // [never, any][]
var entries4 = Object.entries([1, 2, 3, 4]); // [string, number][]

var x2: { [index: string]: any } = {1: 2};
var entries5 = Object.entries(x2); // [string, any][]