javascript - TypeScript - How to add a method outside the class definition - Stack Overflow

typescript, how to add a method outside the class definitionI try to add it on prototype, but errorB.ts

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')
}

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')
}
Share Improve this question asked Jul 18, 2016 at 9:08 sam shasam sha 8529 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

It plains 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')
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744208185a4563206.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信