Skip to content

Commit

Permalink
Add stricter parameter types for Object.values and Object.entries (#2…
Browse files Browse the repository at this point in the history
…0553)

* Add stricter parameter types for Object.values and Object.entries

* Use mapped type and constrain 'T extends object'

* Revert everything but change to parameter types
  • Loading branch information
Andy authored and mhegazy committed Jan 8, 2018
1 parent 2869657 commit 39dfeb0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 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,7 +9,7 @@ interface ObjectConstructor {
* Returns an array of 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.
*/
values(o: any): any[];
values(o: {}): any[];

/**
* Returns an array of key/values of the enumerable properties of an object
Expand All @@ -21,7 +21,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(o: any): [string, any][];
entries(o: {}): [string, any][];

/**
* Returns an object containing all own property descriptors of an object
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/useObjectValuesAndEntries1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ for (var x of Object.values(o)) {
let y = x;
}

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][]
var entries = Object.entries(o);
var entries1 = Object.entries(1);
var entries2 = Object.entries({a: true, b: 2})
var entries3 = Object.entries({})


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

var entries = Object.entries(o); // <-- entries: ['a' | 'b', number][]
var entries = Object.entries(o);
>entries : Symbol(entries, Decl(useObjectValuesAndEntries1.ts, 6, 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, --, --))
>o : Symbol(o, Decl(useObjectValuesAndEntries1.ts, 0, 3))

var entries1 = Object.entries(1); // <-- entries: [string, any][]
var entries1 = Object.entries(1);
>entries1 : Symbol(entries1, 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, --, --))
>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][]
var entries2 = Object.entries({a: true, b: 2})
>entries2 : Symbol(entries2, Decl(useObjectValuesAndEntries1.ts, 8, 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, 8, 31))
>b : Symbol(b, Decl(useObjectValuesAndEntries1.ts, 8, 39))

var entries3 = Object.entries({}) // [never, any][]
var entries3 = Object.entries({})
>entries3 : Symbol(entries3, 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, --, --))
Expand Down
28 changes: 14 additions & 14 deletions tests/baselines/reference/useObjectValuesAndEntries1.types
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,49 @@ var o = { a: 1, b: 2 };
for (var x of Object.values(o)) {
>x : number
>Object.values(o) : number[]
>Object.values : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: any): any[]; }
>Object.values : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: {}): any[]; }
>Object : ObjectConstructor
>values : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: any): any[]; }
>values : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: {}): any[]; }
>o : { a: number; b: number; }

let y = x;
>y : number
>x : number
}

var entries = Object.entries(o); // <-- entries: ['a' | 'b', number][]
var entries = Object.entries(o);
>entries : [string, number][]
>Object.entries(o) : [string, number][]
>Object.entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; }
>Object.entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: {}): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; }
>entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: {}): [string, any][]; }
>o : { a: number; b: number; }

var entries1 = Object.entries(1); // <-- entries: [string, any][]
var entries1 = Object.entries(1);
>entries1 : [string, any][]
>Object.entries(1) : [string, any][]
>Object.entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; }
>Object.entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: {}): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; }
>entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: {}): [string, any][]; }
>1 : 1

var entries2 = Object.entries({a: true, b: 2}) // ['a' | 'b', number | boolean][]
var entries2 = Object.entries({a: true, b: 2})
>entries2 : [string, number | boolean][]
>Object.entries({a: true, b: 2}) : [string, number | boolean][]
>Object.entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; }
>Object.entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: {}): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; }
>entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: {}): [string, any][]; }
>{a: true, b: 2} : { a: boolean; b: number; }
>a : boolean
>true : true
>b : number
>2 : 2

var entries3 = Object.entries({}) // [never, any][]
var entries3 = Object.entries({})
>entries3 : [string, {}][]
>Object.entries({}) : [string, {}][]
>Object.entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; }
>Object.entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: {}): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; }
>entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: {}): [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 @@ -10,9 +10,9 @@ var o = { a: 1, b: 2 };
for (var x of Object.values(o)) {
>x : number
>Object.values(o) : number[]
>Object.values : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: any): any[]; }
>Object.values : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: {}): any[]; }
>Object : ObjectConstructor
>values : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: any): any[]; }
>values : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): T[]; (o: {}): any[]; }
>o : { a: number; b: number; }

let y = x;
Expand All @@ -23,8 +23,8 @@ 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; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; }
>Object.entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: {}): [string, any][]; }
>Object : ObjectConstructor
>entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: any): [string, any][]; }
>entries : { <T>(o: { [s: string]: T; } | { [n: number]: T; }): [string, T][]; (o: {}): [string, any][]; }
>o : { a: number; b: number; }

8 changes: 4 additions & 4 deletions tests/cases/conformance/es2017/useObjectValuesAndEntries1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ for (var x of Object.values(o)) {
let y = x;
}

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][]
var entries = Object.entries(o);
var entries1 = Object.entries(1);
var entries2 = Object.entries({a: true, b: 2})
var entries3 = Object.entries({})

0 comments on commit 39dfeb0

Please sign in to comment.