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
1 Answer
Reset to default 9It 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条)