In one of our systems, we have a lot of UI related javascript that handles menu actions and that look like below
var menuActions = {
"edit-profile": {
Title: "rs_edit_profile",
Action: function(callback){
//some logic here to handle the UI
},
HasPermission: true
},
"delete-profile": {
Title: "rs_edit_profile",
Action: function(callback){
//some logic here to handle the UI
},
HasPermission: true
},
"create-profile": {
Title: "rs_edit_profile",
Action: function(callback){
//some logic here to handle the UI
},
HasPermission: false
}
}
and that is used with switch/if cases like
if(menuAction[actionName] === "edit profile")... which then calls the menuActions[actionName].Action().
We're converting the system to TypeScript right now, which is all new for us, and I'm stuck on how I best should re-organize and convert this specific piece of code.
I don't like how it's done with javascript right now, so if possible, I would like to take the opportunity to change it to something better while doing the conversion to typescript.
My intuition says I should have a collection of MenuAction instances from a class, but I'm not sure as to how I would implement that and then use it.
Should I completely omit the use of a class and instead just use normal objects in an array?
(It might help to know that this is code for an Angular 1.X view controller).
Just regarding the typing, this is one way you could do it:
Whether or not to use class instances is up to you, TS is quite agnostic about design choices like that.
... and if you want the type to be usable for classes to
implement, you have to define it as an interface instead. It's really no other difference between a custom type (type alias) and an interface.