1
I have the following interface:
interface IInterface {
Method1: (param1: any) => any;
Method2: (param2: string[]) => any;
}
I wish that from it I could create objects dynamically that implement and that all methods do the same thing, depending on the parameters passed.
I tried it this way:
const keys = Reflect.ownKeys({} as IInterface);
let obj = {};
for (let key in keys) {
obj[key] = (...params) => {
console.log(`Chamou a função ${key}!`);
return params.length > 0 ? params[0] : false;
};
}
let obj2: IInterface = obj as IInterface; // Aqui eu teria a instancia de IInterface
But to no avail.
I’ve never seen this object before "
Reflect
".– Luiz Felipe
I think you’re confusing the concepts a little bit. Interfaces are created to define a standard contract that different objects must obey. An interface does not define as the method will be implemented. It seems that a simple class definition would solve your problem... Perhaps if you explained better in the question what would be the use of this mechanism you want, a better solution could be proposed.
– Leonardo Lima