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

Infer type when passing a Record to Object.keys() #50096

Closed
MatthewSbar opened this issue Jul 29, 2022 · 2 comments
Closed

Infer type when passing a Record to Object.keys() #50096

MatthewSbar opened this issue Jul 29, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@MatthewSbar
Copy link

MatthewSbar commented Jul 29, 2022

Suggestion

If Object.keys() is passed a Record, the returned array should infer the type based on the type of the Record. Please see example if this is unclear.

🔍 Search Terms

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

object.keys object.keys() record string

✅ Viability Checklist

My suggestion meets these guidelines:

  • [ x ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [ x ] This wouldn't change the runtime behavior of existing JavaScript code
  • [ x ] This could be implemented without emitting different JS based on the types of the expressions
  • [ x ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • [ x ] This feature would agree with the rest of TypeScript's Design Goals.

📃 Motivating Example

Note: Make some kind of change (add a new line at the end for example) to tsconfig.json and save it to see the actual typescript error/red squiggle, it doesn't appear by default
https://stackblitz.com/edit/typescript-tj8tyt?file=index.ts&view=editor

💻 Use Cases

It's a common pattern for myself in React applications to create a Record which maps some enum (by that I mean a string union) to another value, then to use Object.keys() to iterate over the keys of that object and use the key to access the object. The problem is when accessing the object using one of its keys, the key is of type string when it should be guaranteed to be of the type of the index specified for the Record passed into Object.keys()

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 29, 2022
@RyanCavanaugh
Copy link
Member

#45390 (comment)

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

No branches or pull requests

3 participants