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

lib.d.ts Make Object.keys generic #19448

Closed
mohsen1 opened this issue Oct 24, 2017 · 3 comments
Closed

lib.d.ts Make Object.keys generic #19448

mohsen1 opened this issue Oct 24, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@mohsen1
Copy link
Contributor

mohsen1 commented Oct 24, 2017

TypeScript Version: 2.5

Currently Object.keys type is keys<T>(o: {}): string[]; but instead it can be keys<T = {}>(o: T): keyof T; so return type is a union of keys.

Any reason this hasn't been done before? I can make a PR if this is an acceptable change

@mhegazy
Copy link
Contributor

mhegazy commented Oct 24, 2017

Please see #15627

@mhegazy mhegazy added the Duplicate An existing issue was already created label Oct 24, 2017
@mohsen1
Copy link
Contributor Author

mohsen1 commented Oct 24, 2017

This comment
#12253 (comment)

But I'm not convinced. We either have strong typing or not. if someone is depending on runtime data without statically declaring them it is their problem and can case the types to a wide type

/cc @ahejlsberg

Closing as it is by design

@mohsen1 mohsen1 closed this as completed Oct 24, 2017
@kitsonk
Copy link
Contributor

kitsonk commented Oct 24, 2017

If someone is depending on runtime data without statically declaring them it is their problem.

At the moment, TypeScript does not differentiate between own keys and enumerable keys, both which are not covered by Object.keys(). So there is a lot of ways, intentionally or unintentionally Object.keys() typed as suggested would give back bad information.

For example, it breaksdown badly here:

const foo = Object.create({ foo: 'string' });
Object.defineProperty(foo, 'bar', {
  value: 1
});

console.log(Object.keys(foo)); // []
console.log(foo.foo); // 'string'
console.log(foo.bar); // 1

Sounds like a bad 👣 🔫 to me...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants