I would like to be able to associate an enumeration and a type when compiling in TypeScript.
The goal to fix the param of a method with an enum. I try to do that with a closure:
enum EnumType {
TYPE1,
TYPE2
}
interface Type1 {
name: string;
}
interface Type2 {
value: number;
}
function closure(tEnum: EnumType) {
switch(tEnum){
case EnumType.TYPE1:
return (t: Type1) => { console.log(t); }
case EnumType.TYPE2:
return (t: Type2) => { console.log(t); }
}
}
const foo1 = closure(EnumType.TYPE1);
foo1({ name: 'test'}); // here no error Type1 (but there is an error)
foo1({ value: 2}); // here error because not Type1
How do I get this example to work or is there an alternative?