-3

I have the interface, base abstract class that implements this interface and some property in the Angular component that creates the new instance of the base class. This construction doesn't give me errors in the browser (perhaps because it is the JS that can stand anything), but I'm getting the TSLint error

TS2511:Cannot create an instance of the abstract class KitCustomizationBase

If I remove the abstract word this error disappears. As I read in the TS documentation abstract class may content some implemented logic. Can someone explain it?

My interface:

export interface KitCustomizationInterface {
  items: number[];
  indexes: number[];
  counter: number;
}

My base class (I'm getting the same error without the constructor):

import { KitCustomizationInterface } from '...';

export abstract class KitCustomizationBase implements KitCustomizationInterface {
  public items: number[];
  public indexes: number[];
  public counter: number;

  constructor(items: number[], indexes: number[], counter: number) {
    this.items = items;
    this.indexes = indexes;
    this.counter = counter;
  }
}

And the code from my component:

...
let k: KitCustomizationBase = new KitCustomizationBase([], [], 0);
...

Update

Notices for the duplicate markers:

  1. Angular has (because of the TS) different moments about the work with the interfaces.
  2. Moreover, we are talking about the specific TSLint error that doesn't have the results in the Google search.
  3. The link that duplicate markers gave, doesn't provide the explicitly answer. It's more like an answer "go read docs". Docs about what exactly?
halfer
  • 19,824
  • 17
  • 99
  • 186
WeekendMan
  • 561
  • 1
  • 10
  • 25
  • 10
    You can't `new` an abstract class, that's the main point of `abstract`. I also don't see you extending the abstract class. – Günter Zöchbauer Aug 01 '17 at 08:47
  • Possible duplicate of [Interface vs Abstract Class (general OO)](https://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo) – Rax Weber Aug 01 '17 at 08:48
  • Only over the extending and then creating the new instance? – WeekendMan Aug 01 '17 at 08:48
  • @RaxWeber, Angular/Typescript has some details with the interfaces that make difference with the typical mechanism. So, the answer from the topic that you showed is not the answer that I looked for. – WeekendMan Aug 01 '17 at 08:51
  • 1
    @WeekendMan I gave that link because I can see that you don't seem to understand the concept of abstract classes. – Rax Weber Aug 01 '17 at 08:53
  • Omg, so be..... – WeekendMan Aug 01 '17 at 09:00
  • 2
    @WeekendMan the solution is very simple. If you want the class to be abstract then that means you do not want to be able to instantiate it. By implication you should expect the compile time error. The lack of a runtime error is also, as you suspect, due to the fact that JavaScript has no notion of abstract classes. Decide whether you want an abstract class or a concrete class and code accordingly. – Aluan Haddad Aug 01 '17 at 09:06
  • Yes, thanks, I got it. If someone can write this as the answer, I'll mark the answer as the answer for the topic. – WeekendMan Aug 01 '17 at 09:16

1 Answers1

3

What you're doing is a contradiction in itself. When you mark a class as abstract you specifically prohibit instantiation. Then you go and instantiantiate your class. You have to make up your mind.

  • If you don't want to instantiate your class, don't do it. You have to derive a class from your existing class and instantiate it. In TypeScript you also have the option to assign a compatible object to your variable (called structural typing, TypeScript's flavour of duck typing). But then you don't need your constructor. You don't even need the class, your interface will do fine.
  • If you want to instantiate your class, don't make it abstract. The whole intention if an abstract class is to not allow its instantiation.
Sefe
  • 13,731
  • 5
  • 42
  • 55