Skip to content

Commit

Permalink
Merge pull request microsoft#12207 from ethanresnick/precise-object-e…
Browse files Browse the repository at this point in the history
…ntry-types

More precise Object.entries type declarations
  • Loading branch information
mhegazy authored Nov 14, 2016
2 parents a0338d1 + ea33d6e commit 2827b37
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/lib/es2017.object.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ 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: { [s: string]: T }): [string, T][];
entries<T extends { [key: string]: any }, K extends keyof T>(o: T): [keyof T, T[K]][];
entries(o: any): [string, any][];
}
}
11 changes: 8 additions & 3 deletions tests/baselines/reference/useObjectValuesAndEntries1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ for (var x of Object.values(o)) {
let y = x;
}

var entries = Object.entries(o);
var entries1 = Object.entries(1); // <-- entries: [string, any][]
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 entries3 = Object.entries({}) // [never, any][]


//// [useObjectValuesAndEntries1.js]
var o = { a: 1, b: 2 };
for (var _i = 0, _a = Object.values(o); _i < _a.length; _i++) {
var x = _a[_i];
var y = x;
}
var entries = Object.entries(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 entries3 = Object.entries({}); // [never, any][]
16 changes: 15 additions & 1 deletion tests/baselines/reference/useObjectValuesAndEntries1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ for (var x of Object.values(o)) {
>x : Symbol(x, Decl(useObjectValuesAndEntries1.ts, 3, 8))
}

var entries = Object.entries(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 : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
Expand All @@ -30,3 +30,17 @@ var entries1 = Object.entries(1); // <-- entries: [string, any][]
>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, --, --))

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 : 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, --, --))
>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 : 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, --, --))

34 changes: 27 additions & 7 deletions tests/baselines/reference/useObjectValuesAndEntries1.types
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,39 @@ for (var x of Object.values(o)) {
>x : number
}

var entries = Object.entries(o);
>entries : [string, number][]
>Object.entries(o) : [string, number][]
>Object.entries : { <T>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
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 : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
>entries : { <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>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
>Object.entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
>entries : { <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][]
>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 : ObjectConstructor
>entries : { <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
>b : number
>2 : 2

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 : ObjectConstructor
>entries : { <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T, T[K]][]; (o: any): [string, any][]; }
>{} : {}

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

var entries = Object.entries(o);
>entries : [string, number][]
>Object.entries(o) : [string, number][]
>Object.entries : { <T>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
>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 : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; }): [string, T][]; (o: any): [string, any][]; }
>entries : { <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: 4 additions & 2 deletions tests/cases/conformance/es2017/useObjectValuesAndEntries1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ for (var x of Object.values(o)) {
let y = x;
}

var entries = Object.entries(o);
var entries1 = Object.entries(1); // <-- entries: [string, any][]
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 entries3 = Object.entries({}) // [never, any][]

0 comments on commit 2827b37

Please sign in to comment.