I used type because it is immutable. Unlike interface, type cannot be extended later in the code and I wanted to ensure that ReactionMap structure remains same and does not change somewhere else in the code. Thanks for the comment
But wait, at some point, we need to give the user a list of available reactions, right? So we already have an array containing them. We should use that to construct our map:
Hi, Alex. Yes, that is possible, if I fetch them from the database for example. But what when not? For case when I get the reactions list from the user?
For example: If I used Caido tool (security auditing toolkit) and append some non existing reaction, in your case, it would be added to the ReactionMap without check. That would require manual checking or AllowedReactions.
In my case the AllowedReactions are the predefined reactions available on Dev.to, and they are specified in the FinalResponse type. The list of reactions is passed to the user through the reactions property.
This is, I would say, a more advanced project with a lot of data parsing and calculation processes.
In this project, the reactions received on certain dev.to blog post are mapped and then the structure like this is created:
If you add another reaction to the array of existing reactions, it is obviously an existing reaction itself. Saying the types are more true than your data is a fallacy.
A single source of truth reduces the chances of errors and saves time if you have to change something.
You’re assuming we are getting data from a database, no? What if we are hardcoding the reactions into the app and want to use it in multiple locations?
I'm not assuming anyting. At some point, you want to show the reactions in your component, so you will have that array, regardless of how it came to be.
Tech Lead/Team Lead. Senior WebDev.
Intermediate Grade on Computer Systems-
High Grade on Web Application Development-
MBA (+Marketing+HHRR).
Studied a bit of law, economics and design
Location
Spain
Education
Higher Level Education Certificate on Web Application Development
Cool! I was doing this without even acknowledging it. It's nice to give it a name (Map Pattern) so it feels solid. Definetely something everyone should know and apply.
I usually kind of skip the "control" part and just use Record<string, Whatever> and forget about it. I don't even use the extra type ReactionMap, I just straight go
reactions: Record<string, Reaction>
Although yeah, for bigger projects the control should be included without a doubt.
Thanks for the comment and feedback man. Yes, it sounds solid. This is a problem that many people get wrong because they don't think about extensibility.
Full Stack Software Engineer who loves working with C# , Typescript, .Net Core , Node Js ,Angular , Flutter, And Azure Cloud
Love Working with C# , Typescript
Further improvement: Use enum instead of type for the AllowedReactions — which I'd rather call something along the lines of ReactionKind instead. This will improve the developer experience, e.g. for searching references to a specific kind. Btw, always use string enums.
Also, side note: I would add another reason why Record<string, X> is generally something you'd want to avoid. That type is tricky as it behaves as Partial<Record<string, X>> for definition (not every string need be defined), but Record<string, X> for access. This, in turn, results in unsafe access operations that may crash at runtime: Not every valid string is a key that yields a value X.
In general, most Record types that ain't Record<enum, X> should be Partial<Record<...>> instead.
You might even want to define it as a helper type
There are bunch of bad codes there too. For example you can just use Record instead of
You should also separate links according to your article
This is better. Thanks!
good luck mate. Dont stop writing
Plus you can use interface for objects instead of types
I used type because it is immutable. Unlike interface, type cannot be extended later in the code and I wanted to ensure that
ReactionMapstructure remains same and does not change somewhere else in the code. Thanks for the commentIt's not actually accurate I'm afraid do a little research
But wait, at some point, we need to give the user a list of available reactions, right? So we already have an array containing them. We should use that to construct our map:
Hi, Alex. Yes, that is possible, if I fetch them from the database for example. But what when not? For case when I get the reactions list from the user?
For example: If I used Caido tool (security auditing toolkit) and append some non existing reaction, in your case, it would be added to the
ReactionMapwithout check. That would require manual checking orAllowedReactions.In my case the
AllowedReactionsare the predefined reactions available onDev.to, and they are specified in theFinalResponsetype. The list of reactions is passed to the user through the reactions property.This is, I would say, a more advanced project with a lot of data parsing and calculation processes.
In this project, the reactions received on certain
dev.toblog post are mapped and then the structure like this is created:If you are interested you can check the code in the repository.
You can also try it out here: dev-to-rater.xyz
If you add another reaction to the array of existing reactions, it is obviously an existing reaction itself. Saying the types are more true than your data is a fallacy.
A single source of truth reduces the chances of errors and saves time if you have to change something.
You’re assuming we are getting data from a database, no? What if we are hardcoding the reactions into the app and want to use it in multiple locations?
I'm not assuming anyting. At some point, you want to show the reactions in your component, so you will have that array, regardless of how it came to be.
Good post, thanks for sharing! 😃👍🏻
You're welcome! If you want to view it in a real action, feel free to do it in the repository.
Specifically, the files are:
Thanks for reading :)
Nice
Glad you liked it
Cool! I was doing this without even acknowledging it. It's nice to give it a name (Map Pattern) so it feels solid. Definetely something everyone should know and apply.
I usually kind of skip the "control" part and just use
Record<string, Whatever>and forget about it. I don't even use the extra typeReactionMap, I just straight goAlthough yeah, for bigger projects the control should be included without a doubt.
Nice post my man.
Thanks for the comment and feedback man. Yes, it sounds solid. This is a problem that many people get wrong because they don't think about extensibility.
It's absolutely correct, and it's very easy to see which properties are in this, manageable
Gonna use this
Thanks for feedback. Glad it was useful
Great one!
Thank you!
Good post very helpful and thanks to all the people in the comments that are suggesting and improving it
Yes, that is key. To help each other
Thanks, this a great tip!
You're welcome!
Further improvement: Use
enuminstead oftypefor theAllowedReactions— which I'd rather call something along the lines ofReactionKindinstead. This will improve the developer experience, e.g. for searching references to a specific kind. Btw, always use string enums.Also, side note: I would add another reason why
Record<string, X>is generally something you'd want to avoid. That type is tricky as it behaves asPartial<Record<string, X>>for definition (not everystringneed be defined), butRecord<string, X>for access. This, in turn, results in unsafe access operations that may crash at runtime: Not every validstringis a key that yields a valueX.In general, most
Recordtypes that ain'tRecord<enum, X>should bePartial<Record<...>>instead.You might even want to define it as a helper type
for convenience.
You left some room for improvements:
How about
Why not just define a common type that include all reactions and use it in both places