0

I know that interface are just contracts and they do not provide implementations.For example when i have

interface ISomething {
  exportJson():boolean;
}


export class AppComponent implements ISomething {

}

to implement the interface i will do the following

export class AppComponent implements ISomething {
  exportJson(): boolean {
    throw new Error('Method not implemented.');
  }
}

when i implement this interface i want specific logic inside - automatically for example

exportJson(): boolean {
    let getJsonPath = '';
    // some oher IMPLEMENTATION LOGIC
    return true;
  }

if i try with classes


class Something {
  exportJson() {
    let getJsonPath = '';
    // some oher IMPLEMENTATION LOGIC
    return true;
  }
}


export class AppComponent extends Something {
}

then i will have the implementation of the exportJson but there is not contract between, by cotnract i mean that the exportJson method must be implemented - we get error when we are not implementing some method from the interface.

Is there way for this ?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
JohnJS
  • 35
  • 3
  • Why not something like a [template method](https://en.wikipedia.org/wiki/Template_method_pattern)? Why much around with interfaces? – VLAZ Aug 11 '22 at 18:17
  • Does this answer your question? [Typescript: How to extend two classes?](https://stackoverflow.com/questions/26948400/typescript-how-to-extend-two-classes) – Matthieu Riegler Aug 11 '22 at 18:23
  • To the person that closed this question: Where you saw in this question that i need to extend from two classes ?! I asked other thing – JohnJS Aug 11 '22 at 22:15
  • Your question is not entirely clear, why did you remove `implements ISomething` from `class Something`? – Kokodoko Aug 18 '22 at 11:11

0 Answers0