Skip to content

add stronger typings for Object.keys #30314

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

Closed
wants to merge 1 commit into from

Conversation

Arcath
Copy link

@Arcath Arcath commented Mar 11, 2019

Object.keys is currently typed as outputting string[] which is technically correct but causes issues when you forxample want to use the key with the original object if that object does not have an index signature.

const foo = {
   a: {
    bar: 'something'
  },
  b: {
    bar: 'else'
  }
}

Object.keys(foo).map((key) => {
  return foo[key].bar // Not valid because `foo` has no index signature
})

This is a bit annoying as key can only be a valid index of foo but TS won't let you use it as such.

Using this PR the above code works as key has the type keyof foo which is usable to index foo.

I have to apologise as the tests aren't passing at the moment. I'm struggling to see where they are failing and could do with some help. I also can't sign in to cla.opensource.microsoft.com to sign the CLA.

@cpplearner
Copy link

See #12253 (comment).

@Arcath
Copy link
Author

Arcath commented Mar 11, 2019

Thanks, I should have looked into it first. Stick with my function for now.

@Arcath Arcath closed this Mar 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants