我正在寻找一种在类中设置名称字符串的方法,并在构造函数级别的抽象类中使用它,意思是不在函数中使用。我不能打开构造函数,因为我使用的是typedi。
Uncaught : this.name不是函数
abstract class Root {
abstract name(): string
notFoundError = new Error(`${this.name()} not found`)
}
class Use extends Root {
name = () => 'User'
}
const x = new Use()
throw x.notFoundError我不是在找这个:
abstract class Root {
abstract name: string
notFoundError = () => new Error(`${this.name} not found`)
}
class Use extends Root {
name = 'User'
}
const x = new Use()
throw x.notFoundError()对notFoundError不是函数感兴趣。
发布于 2019-11-19 20:50:29
有一个想法:
const Root = (name: string) => {
abstract class Root {
name: string = name
notFoundError = new Error(`${this.name} not found`)
}
return Root
}
class Use extends Root('User') {
}
const x = new Use()
throw x.notFoundError发布于 2019-11-19 20:51:57
而不是name = 'User'或name = () => 'User'使用name() { return 'User' }。
abstract class Root {
abstract name(): string
notFoundError = new Error(`${this.name()} not found`)
}
class Use extends Root {
name() { return 'User' }
}
const x = new Use()
throw x.notFoundErrorhttps://stackoverflow.com/questions/58942458
复制相似问题