Reputation: 32808
I am creating something similar to an ENUM in a base class / different file:
ExamStatusId = {
All: {
elem: this.examStatusSelectId, // << refers to a string
text: 'Exam Status: All',
val: 0
},
Current: {
elem: this.examStatusSelectId, // << refers to a string
text: 'Exam Status: Current',
val: 1
}
}
Once this is defined I call a function like this inside of another class / file:
page.isSelectedValue(page.ExamStatusId.All);
Here's the function that's in yet another class / file:
isSelectedValue (data) {
var title = this.getTitle(data.id);
var valueString = data.val.toString();
it('Check for ' + data.text, function () {
expect(data.elem.getAttribute("value")).toBe(valueString);
});
}
This code works but can someone tell me if this is a better way for me to pass the data I need to the isSelectedValue
function using Typescript? I already use Typescript for the code and would like to make the most of all the features it offers.
Also how can I ensure that what I pass to that function has all the parameters of elem, text and val ?
Upvotes: 0
Views: 481
Reputation: 2591
enums are tedious sometimes when it comes to get the value of the same, here is what i user in that case
export class urlMapper {
//contains names of the api url's
static homeUrl:string = '/home';
static loginUrl:string ='/auth';
static userUrl:string ='/user';
static logoutUrl:string ='/logout';
static storeUrl:string="/store";
}
Upvotes: 0
Reputation: 1082
Typescript has built-in enum syntax:
enum EnumName {
member1,
member2,
member3
}
EnumName then becomes a named type, so you could have a function:
((foo: EnumName) => { })(EnumName.member1);
In order to meet your specific request, perhaps you could do something like:
enum Status {
All,
Current
}
ExamStatusID = {
elem: this.examStatusSelectId
val: (status: Status) => {
switch(status) {
case Status.All: //etc
}
}
}
Upvotes: 5
Reputation: 14519
It looks like you want something like the code below. At least to me it looks like you are not using enums but more something like object constants? This is something which can be done with a extra interface. The nice thing about this you still have typechecking in your isSelectedValue function!
interface IElement {
elem: string;
text: string;
val: number
}
var ExamStatusId = {
All: <IElement>{
elem: this.examStatusSelectId, // << refers to a string
text: 'Exam Status: All',
val: 0
},
Current: <IElement>{
elem: this.examStatusSelectId, // << refers to a string
text: 'Exam Status: Current',
val: 1
}
}
function isSelectedValue (data: IElement) {
}
isSelectedValue(ExamStatusId.All);
Upvotes: 1