【发布时间】:2020-06-27 00:23:50
【问题描述】:
我有一个类Foo,我想用我拥有的一些文字对象动态扩展它。如何在单独的界面中保持this 键入而不手动重复文字对象的键?
const someLiteralObject = {
key1: 'foo',
key2: 'bar',
// ...lots of more keys
}
class Foo {
constructor(){
Object.assign(this, someLiteralObject)
}
test() {
console.log(this.key1); //error: Property 'key1' does not exist on type 'Foo'
}
}
const test = new Foo();
console.log(test.key1); //error: Property 'key1' does not exist on type 'Foo'
【问题讨论】:
标签: typescript class this extends