Skip to content

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
@RyanCavanaugh
Copy link
Member

@typescript-bot
Copy link
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants