admin管理员组文章数量:1400167
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')
}
本文标签: javascriptTypeScriptHow to add a method outside the class definitionStack Overflow
版权声明:本文标题:javascript - TypeScript - How to add a method outside the class definition - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744208185a2595291.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论