2

How can i properly define a abstract method in a abstract class ? I want that IDE tell me that implementation of that abstract method is a must.

I tried the following, but without success:

export abstract class MyAbstractClass {

   /**
    * @abstract
    */
   public submit() {
      throw new Error('This class must be implemented')
   }
}

The question is: how can we make the IDE to complain if you don't implement the abstract method ?

Andrei Todorut
  • 4,260
  • 2
  • 17
  • 28
  • 2
    Possible duplicate of [Declaring abstract method in TypeScript](https://stackoverflow.com/questions/13333489/declaring-abstract-method-in-typescript) – idream1nC0de Aug 27 '18 at 13:33
  • 1
    can't you just add the `abstract` keyword instead of `public` ? – jonatjano Aug 27 '18 at 13:33
  • 1
    `abstract makeSound(input : string) : string;` where `input` is a param, first `string` is typechecking and second `string` is the return type – popeye Aug 27 '18 at 13:34
  • @JacobHeater is not duplicated. I'm just using the syntax from there, but i want to know if there's a way to integrate it with IDE (Visual Studio Code for example) – Andrei Todorut Aug 27 '18 at 13:38
  • 1
    @AndreiTodorut That would require you to actually correctly define the `abstract` method, therefore making this question a duplicate. `How can i properly define a abstract method in a abstract class ?` – idream1nC0de Aug 27 '18 at 13:45
  • @JacobHeater, updated the question title :) – Andrei Todorut Aug 27 '18 at 13:48

1 Answers1

2

Try this:

export abstract class MyAbstractClass {
   // we shouldn't declare the body of abstract method
   abstract submit(): void;
}

//...

class MyClass extends MyAbstractClass {
}

And you will see ide complain: enter image description here

What you're doing in your question example - is a well known ES6 hack for emulating abstract classes. But TypeScript supports abstract classes out of the box. For more details, you can check the official documentation on classes.

Also created a stackblitz demo, you can check it out.

shohrukh
  • 2,989
  • 3
  • 23
  • 38
  • @sherlock92, ok but when you separate those 2 classes in files the IDE will not complain about missing implementation for abstract method. – Andrei Todorut Aug 28 '18 at 05:57
  • I don't see any issues here. Check the updated [stackblitz demo](https://stackblitz.com/edit/typescript-jirhhz?file=index.ts) - I've separated classes in two files and still get complain. – shohrukh Aug 28 '18 at 06:21