How can I create a wrapper class for a collection of elements in an enumeration?
export class Flags<ENUMERATION> {
items = new Set<ENUMERATION>();
enu; // what type ?
constructor(enu) { // what type ?
this.enu=enu;
}
set(id:ENUMERATION) { this.items.add(id); return this; }
// For use: determine if a specific value is part of the enum or not
setChecking(id:string):boolean{
if (id in this.enu){
let item = this.enu[id];
this.items.add(item);
return true;
}
return false;
}
// .....
}
Example:
enum Props{ One, Two, Three };
let fls=new U.Flags<Props>(Props);
fls.set(Props.One);
fls.set("asdf"); // TypeScript detects the wrong value
fls.set(Props.Two);
if (!fls.setChecking("xxxx")) // Check external string against the enum set
throw error
Question: What are the types of the 'enu' property and the parameter in the constructor, what is the type of the enum object?
If we specify the type in the constructor, we could write:
let fls=new U.Flags(Props);
(TypeScript would infer the type from the constructor specification)