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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difference between { [key in string]: any } and Record<string, any> #141

Closed
mesqueeb opened this issue Sep 1, 2020 · 2 comments
Closed
Labels
question More info is requested

Comments

@mesqueeb
Copy link
Contributor

mesqueeb commented Sep 1, 2020

馃 Question

My question is about the difference between { [key in string]: any } and Record<string, any> and their use cases.

Describe your question

I often have cases where I allow people to pass any plain JS object and will do something with that object, and pass back a new object based on the original, but with some changes.

some examples include:

Often I differentiate via JS between "plain objects" and "class instances" by looking at the prototype.

Now, currently in most cases I was just using a type like this for plain objects:

{ [key: string]: any }

But since there were merge issues, you've explained before it's better to use:

{ [key in string]: any }

so in many libraries I've been slowly replacing the types to that.

However, it seems many users use Record<string, any> as a "plain object" type...

So considering my use cases above, what do you recommend I use?

Search tags, topics

#typescript #types #plain-objects #plain-object-type #object-type #record #faq #question

@mesqueeb
Copy link
Contributor Author

mesqueeb commented Sep 2, 2020

I believe the answer is

Record<string, any>
==
{ [key in string]: any }

so it's a matter of preference / readability

@mesqueeb mesqueeb closed this as completed Sep 2, 2020
@millsp
Copy link
Owner

millsp commented Sep 3, 2020

Yep, no difference

@millsp millsp added the question More info is requested label Sep 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question More info is requested
Projects
None yet
Development

No branches or pull requests

2 participants