假设有一个接口:
interface ServiceDataIf {
somethingToDo(): void;
mayBeAdd(arg: any): void;
mayBeGet(name: string): any;
readonly someVal: string;
anotherVal: string;
[name: string]: any;
}
Run Code Online (Sandbox Code Playgroud)
如何在类中实现该接口:
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 …Run Code Online (Sandbox Code Playgroud) typescript ×1