Call an abstract method with prototype

Mazen Alkatlabee

I have an abstract class with an abstract method. I discovered that Typescript won't throw any error if I called the method with prototype and call. For exmaple:

abstract class Human{
    age: string;
    constructor(age: string){
        this.age = age;
    }
    abstract name(): void
}

class Human1 extends Human{
    constructor(age: string){
        super(age);
        this.name()
        
    }
name(): void {
    Human.prototype.name.call(this); // this is allowed despite that name is undefined/abstract
    let a = "do something";
    }
}

let a = new Human1("23")

Is there a way to make Typescript throw an error here?

Bergi

I discovered that Typescript won't throw any error if I called the method with prototype and call.

Yes, TypeScript is pretty bad at typing prototype objects. It thinks that Human.prototype has the type Human1, and for that it only considers the public interface. The abstract modifier is only affecting subclass implementations.

Is there a way to make Typescript throw an error here?

Yes, don't use .call to call the parent method. There's the super keyword for that:

name(): void {
    super.name(); // this errors
    let a = "do something";
}

(playground demo)

This does not compile but throws the error "Abstract method 'name' in class 'Human' cannot be accessed via super expression. (ts 2513)" as expected.

1: Notice also that it thinks Human.prototype.age is a number, and that Human.prototype.constructor is any Function and not typeof Human.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call non abstract method in a abstract class?

From Dev

JS - Call to static method with prototype

From Dev

How to call a method in an abstract class

From Dev

How to call abstract class method

From Dev

Simpler / better way to call shadowed prototype method?

From Dev

Nature of the call method on Function.prototype

From Dev

jQuery - how to call method/prototype function

From Dev

Call Object.prototype method on Global Scope

From Dev

Typescript - call abstract method from abstract constructor or provide equivalent functionality

From Java

How to call abstract method from abstract class called by inherit class

From Dev

Call through the base class abstract method

From Dev

How to abstract method call in lamba expression?

From Java

Is it valid to call an abstract method on the super class in java

From Java

getClass() in abstract class gives Ambiguous method call

From Java

Is it OK to call abstract method from constructor in Java?

From Java

How to force subclass to call an abstract implemented method

From Dev

java call method in constructor and abstract class?

From Dev

How to call a base method that is declared as abstract override

From Dev

How to call a method in an abstract class properly

From Dev

Abstract class constructor call overridable method

From Dev

Unable to call abstract class method from interface

From Dev

Is it possible to call child method from abstract?

From Dev

Call a method from abstract class c#

From Dev

C++ Call an abstract method from a static method using *this

From Java

Ensure method A is called after every call of B (an abstract implemented method)?

From Dev

Call method in abstract class after method of extending class?

From Dev

JS - why would child class method call parent class method using Parent.prototype.method.call?

From Dev

Using call() method vs setting prototype property for inheritance

From Dev

Prototype chain: call "super" method over multiple levels

Related Related

  1. 1

    How to call non abstract method in a abstract class?

  2. 2

    JS - Call to static method with prototype

  3. 3

    How to call a method in an abstract class

  4. 4

    How to call abstract class method

  5. 5

    Simpler / better way to call shadowed prototype method?

  6. 6

    Nature of the call method on Function.prototype

  7. 7

    jQuery - how to call method/prototype function

  8. 8

    Call Object.prototype method on Global Scope

  9. 9

    Typescript - call abstract method from abstract constructor or provide equivalent functionality

  10. 10

    How to call abstract method from abstract class called by inherit class

  11. 11

    Call through the base class abstract method

  12. 12

    How to abstract method call in lamba expression?

  13. 13

    Is it valid to call an abstract method on the super class in java

  14. 14

    getClass() in abstract class gives Ambiguous method call

  15. 15

    Is it OK to call abstract method from constructor in Java?

  16. 16

    How to force subclass to call an abstract implemented method

  17. 17

    java call method in constructor and abstract class?

  18. 18

    How to call a base method that is declared as abstract override

  19. 19

    How to call a method in an abstract class properly

  20. 20

    Abstract class constructor call overridable method

  21. 21

    Unable to call abstract class method from interface

  22. 22

    Is it possible to call child method from abstract?

  23. 23

    Call a method from abstract class c#

  24. 24

    C++ Call an abstract method from a static method using *this

  25. 25

    Ensure method A is called after every call of B (an abstract implemented method)?

  26. 26

    Call method in abstract class after method of extending class?

  27. 27

    JS - why would child class method call parent class method using Parent.prototype.method.call?

  28. 28

    Using call() method vs setting prototype property for inheritance

  29. 29

    Prototype chain: call "super" method over multiple levels

HotTag

Archive