The goal is to convert a string (case-insensitive) to an enum value, or undefined if not present. Here is what I have so far:
// enum generated by graphql codegen
enum ItemType {
Club = 'CLUB',
Comment = 'COMMENT',
// and more...
}
function toItemType(type:string):ItemType|undefined {
return ItemType[type as keyof typeof ItemType]
}
console.log(toItemType("Comment")) // COMMENT
console.log(toItemType("comment")) // undefined
console.log(toItemType("COMMENT")) // undefined
This code appears to key off the enum-name Comment, not the enum-value "COMMENT".
The enums are generated by graphql-codegen, which appears to CammelCase the name. The enum string-values are always UPPER_CASE.
I also do want it to return undefined if values are not present (either upper or lower-case), so that behavior is intended.