M
M
MrZillaGold2021-01-06 15:38:42
typescript
MrZillaGold, 2021-01-06 15:38:42

How to correctly type a method of a child class in the parent?

There is a class Athat extends the class B. The class Ahas a method test. When calling a method testin a class, Bwe get a warning:

TS2339: Property 'test' does not exist on type 'B'.


I do not quite understand how to define this method correctly.

class A extends B {
   test(): void {}
}

class B {
   handle(): void {
      this.test();
   }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RAX7, 2021-01-06
@MrZillaGold

https://www.typescriptlang.org/docs/handbook/class...

abstract class B {
  abstract test(): void;

  handle(): void {
    this.test();
  }
}

class A extends B {
  test(): void {}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question