Issue
I am getting some serialized information into my angular application on form if JSON. I would like to check if event property name is one of my preDefined strings.
Event name type:
EventName =
'appInfo' |
'connectivity' |
'location' |
'pushNotification' |
'newVersion';
const foo: EventName = 'appInfo';
const bar: EventName = 'appInfos'; // error
const baz: EventName = JSON.parse('appInfos'); // no error
// validation
const nameValid = [
'appInfo',
'connectivity',
'location',
'pushNotification',
'newVersion'
].includes(baz) // works, but I would need to change things here and in type if something changes
Solution
Based on answer from @bherbruck I changed EventName from type to enum. So my enum looks like
enum EventName {
AppInfo = 'appInfo',
Connectivity = 'connectivity',
Location = 'location',
PushNotification = 'pushNotification',
NewVersion = 'newVersion'
}
and validation is
... && Object.values(EventName).includes(this.name);
Answered By - peter.cambal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.