Suppose, there is an interface:
interface ServiceDataIf {
somethingToDo(): void;
mayBeAdd(arg: any): void;
mayBeGet(name: string): any;
readonly someVal: string;
anotherVal: string;
[name: string]: any;
}
How to implement this interface in a class:
class ServiceDataImpl1 implements ServiceDataIf {
// easy with properties
get someVal(): string {
const result = /* somehow get it */;
return result;
}
constructor() {}
set anotherVal(v: string): void {
// remember somewhere v
}
// plain methods easy as well
somethingToDo(): void { /* do something */ }
mayBeAdd(arg: any): void { /* do another something */ }
mayBeGet(name: string): any {
const result = /* somehow get it */;
return result;
}
// How to implement this? [name: string]: any;
}
Methods and properties are ok, is there any way to implement by key
accessor with a class? To clarify, can that [name: string]: any
accessor be implemented as a class method just like get/set for properties?
So that it can be used like this:
const myImpl = new ServiceDataImpl1();
// read
const val1 = myImpl['something'];
// write
myImpl['something'] = 256;