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 2 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
3 changes: 2 additions & 1 deletion 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 extends { [key: string]: any }, K extends keyof T>(o: T): [keyof T, T[K]][];
entries<T>(o: Array<T>): [string, T][];
entries<T extends { [key: string]: any }, K extends keyof T>(o: T): [keyof T & string, 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 @@ -210,6 +210,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 & string)[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key of T & string is just string. so this does not help you much.

keys<T>(o: Array<T>): string[];
keys<T>(o: T): (keyof T)[];

Copy link
Contributor Author

@ethanresnick ethanresnick Nov 15, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhegazy Edit: sorry, I misread your comment.

key of T & string is often ("key1" & string) | ("key2" & string) | ... | ("keyn" & string), not just string.

The intersection is needed because sometimes keyof T is string | number, and we want to exclude number as a possible type for the entry keys. See this test case: https://github.com/Microsoft/TypeScript/pull/12253/files#diff-3ed8d911aae864ffc1d88e62bcb8dc47R15

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These intersections do make the intellisense popups uglier, but they're required for correctness.

Could the solution to that ugliness be for the compiler to do some work simplifying intersections (and unions)? E.g., 'a' & string could just be simplified to to 'a' (or, more generally, type & subType could just be simplified to subType). Any efforts in that direction could also help with the bug I brought up in #11426. Would this be that hard? I'd kind of expect simplifying logical formulations to be a somewhat solved problem with theorem provers etc, though I haven't looked into it, so maybe that's incredibly naive. Or maybe it's way too slow to apply here in the general case. Still, specific simplification rules might be a good idea.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would rather leave it string[] then. adding the & string removes any type safety the literal types give you.

Copy link
Contributor Author

@ethanresnick ethanresnick Nov 16, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding the & string removes any type safety the literal types give you.

I don't really understand that. Why would you lose type safety? The & string version still only allows certain strings, rather than all strings, which is more safe.

Let me give the real code example that motivated these two PRs:

const loggers = {
  info: ...,
  warn: ...,
  error: ...
}

Object.entries(loggers).forEach(([name, logger]) => {
  // In here, I want typeof name to be "info" | "warn" | "error", so that 
  // the line below will fail if I add a key to loggers by mistake that
  // doesn't have a corresponding method on console. (This in fact
  // happened: I originally had a debug logger, but only later realized
  // that there is no console.debug() on the node console object.)

  logger.log = console[name].bind(console);
})

To make that code work, string[] isn't enough for typeof name. The change merged in #12207 meanwhile, works perfectly, but fails in the somewhat obscure case that motivated the & string addition in this PR. Using & string, the type of name is inferred as ("info" & string) | ("warn" & string) | ("error" & string). That should, if I understand correctly, be equivalent to "info" | "warn" | "error". However, I instead get an error. That seems like a separate bug, though (opened issue #12289 for it), also tied to simplification.

Going forward... if #12289 were addressed, I think the & string would be the right type signature for Object.keys/entries. But, if #12289 can't be easily addressed, maybe it's better to ignore case that & string was meant to solve, rather than giving up on having key-named literals entirely. If we did that, the signature would be:

keys<T>(o: Array<T>): string[];
keys<T extends { [key: string]: any }>(o: T): (keyof T)[];
keys(o: any): string[];

Then, Object.keys(loggers) would correctly be ("info" | "warn" | "error")[], but let y: { [x: string]: any } = {}; Object.keys(y) would be (string | number)[] rather than string[]. Frankly, that sounds like a net win.

keys(o: any): string[];
}

Expand Down
21 changes: 15 additions & 6 deletions tests/baselines/reference/useObjectValuesAndEntries1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ for (var x of Object.values(o)) {
let y = x;
}

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

// type below should be [string | (string & number), any] NOT [string | number, any]
var x2: { [index: string]: any } = {1: 2};
var entries5 = Object.entries(x2);


//// [useObjectValuesAndEntries1.js]
Expand All @@ -18,7 +23,11 @@ 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 entries = Object.entries(o); // <-- entries: [('a' & string) | ('b' & string), 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 entries2 = Object.entries({ a: true, b: 2 }); // [('a' & string) | ('b' & string), number | boolean][]
var entries3 = Object.entries({}); // [string, any][]
var entries4 = Object.entries([1, 2, 3, 4]); // [string, number][]
// type below should be [string | (string & number), any] NOT [string | number, any]
var x2 = { 1: 2 };
var entries5 = Object.entries(x2);
40 changes: 29 additions & 11 deletions tests/baselines/reference/useObjectValuesAndEntries1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,48 @@ for (var x of Object.values(o)) {
>x : Symbol(x, Decl(useObjectValuesAndEntries1.ts, 3, 8))
}

var entries = Object.entries(o); // <-- entries: ['a' | 'b', number][]
var entries = Object.entries(o); // <-- entries: [('a' & string) | ('b' & string), 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' & string) | ('b' & string), 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][]
var entries3 = Object.entries({}) // [string, 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, --, --))

// type below should be [string | (string & number), any] NOT [string | number, any]
var x2: { [index: string]: any } = {1: 2};
>x2 : Symbol(x2, Decl(useObjectValuesAndEntries1.ts, 14, 3))
>index : Symbol(index, Decl(useObjectValuesAndEntries1.ts, 14, 11))

var entries5 = Object.entries(x2);
>entries5 : Symbol(entries5, Decl(useObjectValuesAndEntries1.ts, 15, 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, 14, 3))

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

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][]; }
var entries = Object.entries(o); // <-- entries: [('a' & string) | ('b' & string), number][]
>entries : [("a" & string) | ("b" & string), number][]
>Object.entries(o) : [("a" & string) | ("b" & string), number][]
>Object.entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T & string, 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 & string, 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 & string, 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 & string, 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][]; }
var entries2 = Object.entries({a: true, b: 2}) // [('a' & string) | ('b' & string), number | boolean][]
>entries2 : [("a" & string) | ("b" & string), number | boolean][]
>Object.entries({a: true, b: 2}) : [("a" & string) | ("b" & string), number | boolean][]
>Object.entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T & string, 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 & string, 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][]; }
var entries3 = Object.entries({}) // [string, any][]
>entries3 : [string, any][]
>Object.entries({}) : [string, any][]
>Object.entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T & string, 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 & string, 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 & string, 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 & string, T[K]][]; (o: any): [string, any][]; }
>[1, 2, 3, 4] : number[]
>1 : 1
>2 : 2
>3 : 3
>4 : 4

// type below should be [string | (string & number), any] NOT [string | number, any]
var x2: { [index: string]: any } = {1: 2};
>x2 : { [index: string]: any; }
>index : string
>{1: 2} : { 1: number; }
>2 : 2

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

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 : ["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][]; }
>entries : [("a" & string) | ("b" & string), number][]
>Object.entries(o) : [("a" & string) | ("b" & string), number][]
>Object.entries : { <T>(o: T[]): [string, T][]; <T extends { [key: string]: any; }, K extends keyof T>(o: T): [keyof T & string, 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 & string, T[K]][]; (o: any): [string, any][]; }
>o : { a: number; b: number; }

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

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

// type below should be [string | (string & number), any] NOT [string | number, any]
var x2: { [index: string]: any } = {1: 2};
var entries5 = Object.entries(x2);