TypeScript - How to add a method outside the class definition

sam sha

typescript, how to add a method outside the class definition

I try to add it on prototype, but error

B.ts

export class B{
    name: string = 'sam.sha'
}

//Error:(21, 13) TS2339: Property 'say' does not exist on type 'B'.
B.prototype.say = function(){
    console.log('define method in prototype')
}
Nitzan Tomer

It complains because you did not define that B has the method say.
You can:

class B {
    name: string = 'sam.sha'
    say: () => void;
}

B.prototype.say = function(){
    console.log('define method in prototype')
}

Or:

class B {
    name: string = 'sam.sha'
}

interface B {
    say(): void;
}

B.prototype.say = function(){
    console.log('define method in prototype')
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

TypeScript - How to add a method outside the class definition

From Dev

inner class method definition outside of a template class

From Dev

Reference instance method outside class definition

From Dev

How to 'return' outside function definition in typescript

From Dev

Python - declare method name in dictionary with method definition in outside class

From Dev

Python - declare method name in dictionary with method definition in outside class

From Dev

How to call method outside class

From Dev

Move method definition for template nested class outside declaration

From Dev

How do I add a static method to my TypeScript class?

From Dev

How do I add a static method to my TypeScript class?

From Dev

TypeScript class decorators - add class method

From Dev

How to add a typescript definition file to a npm package?

From Dev

How do I extend a TypeScript class definition in a separate definition file?

From Dev

Partial Template specialization definition outside of class definition

From Dev

Partial Template specialization definition outside of class definition

From Dev

Python: How to invoke a method from outside a class?

From Dev

friend not allowed outside of a class definition

From Dev

Variable assignment outside class definition

From Dev

Move function outside class definition

From Dev

How to add attributes from outside of the class?

From Dev

How do I define a Python method that I only want to use in the __init__ method, i.e., that is not accessible from outside the class definition?

From Dev

Typescript decorate class add new method

From Dev

Dynamic method definition in a class

From Dev

regarding a method definition in a class

From Dev

How can one use a class method in a class attribute definition

From Dev

Mocking a method outside of a class

From Dev

Declare method outside of class

From Dev

how to add objects to array of objects in class, from outside of the class in Java

From Dev

In Typescript how to do I make a class definition available

Related Related

  1. 1

    TypeScript - How to add a method outside the class definition

  2. 2

    inner class method definition outside of a template class

  3. 3

    Reference instance method outside class definition

  4. 4

    How to 'return' outside function definition in typescript

  5. 5

    Python - declare method name in dictionary with method definition in outside class

  6. 6

    Python - declare method name in dictionary with method definition in outside class

  7. 7

    How to call method outside class

  8. 8

    Move method definition for template nested class outside declaration

  9. 9

    How do I add a static method to my TypeScript class?

  10. 10

    How do I add a static method to my TypeScript class?

  11. 11

    TypeScript class decorators - add class method

  12. 12

    How to add a typescript definition file to a npm package?

  13. 13

    How do I extend a TypeScript class definition in a separate definition file?

  14. 14

    Partial Template specialization definition outside of class definition

  15. 15

    Partial Template specialization definition outside of class definition

  16. 16

    Python: How to invoke a method from outside a class?

  17. 17

    friend not allowed outside of a class definition

  18. 18

    Variable assignment outside class definition

  19. 19

    Move function outside class definition

  20. 20

    How to add attributes from outside of the class?

  21. 21

    How do I define a Python method that I only want to use in the __init__ method, i.e., that is not accessible from outside the class definition?

  22. 22

    Typescript decorate class add new method

  23. 23

    Dynamic method definition in a class

  24. 24

    regarding a method definition in a class

  25. 25

    How can one use a class method in a class attribute definition

  26. 26

    Mocking a method outside of a class

  27. 27

    Declare method outside of class

  28. 28

    how to add objects to array of objects in class, from outside of the class in Java

  29. 29

    In Typescript how to do I make a class definition available

HotTag

Archive