2016-07-18 4 views
0

машинопись, как добавить метод вне определения классаМашинопись - Как добавить метод вне определения класса

я пытаюсь добавить его в прототипе, но ошибка

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

ответ

5

Он жалуется, потому что вы не определили, что B имеет способ say.
Вы можете:

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

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

Или:

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

interface B { 
    say(): void; 
} 

B.prototype.say = function(){ 
    console.log('define method in prototype') 
} 
Смежные вопросы