有以下脚本
class Assignable {
constructor(properties: any) {
Object.keys(properties).map((key: string) => {
this[key as string] = properties[key]
})
}
}
抛出这个错误
Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'Assignable'.ts
知道为什么会发生这种情况以及如何解决它
答案 0 :(得分:1)
你只需要在你的类上创建索引:
class Assignable {
[key: string]: any;
constructor(properties: any) {
Object.keys(properties).forEach(key => {
this[key] = properties[key]
});
}
}