I am looking to enforce a Generic type to be strictly a numerical enum. This means that the values of the Enum should only be numbers. Here is an example:
export type TNumberEnum<Enum> = { [key in keyof Enum]: number };
enum sample {
AA,
BB,
CC = 'cc'
}
class SampleClass<CustomEnum extends TNumberEnum<CustomEnum>> {
constructor( private value: CustomEnum ){}
public sampleFunction(): CustomEnum {
return this.value;
}
}
let a = new SampleClass<sample>( sample.CC ); // Should fail since keyof sample has string value
// Could valueOf and toString cause any issues?
When passing a custom Enum to the SampleClass, I want to ensure that the Generic Enum is of a numerical enum type. Is this achievable?