【发布时间】:2021-07-06 02:08:16
【问题描述】:
我想要一个只接受在 mobx 中声明的状态的函数。我目前有这个代码。
class DialogStore {
status: boolean = false;
title: string = '';
content: string = '';
btnText: string = '';
icon: JSX.Element | string = '';
type: 'success' | 'error' = 'success';
constructor() {
makeAutoObservable(this);
}
toggle(status: boolean) {
this.status = status
}
success(content: string) {
this.type = 'success';
this.status = true;
this.content = content;
}
error(content: string) {
this.type = 'error';
this.status = true;
this.content = content;
}
}
我想添加这样的动态函数:
update(payload: TypeOfState) {
Object.keys(payload).forEach(property => {
this[property] = payload[property];
})
}
我试图将any 作为我的有效负载类型,但它给了我这个错误Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'DialogStore'。我希望有效载荷只接受我在顶部声明的状态。
【问题讨论】:
标签: reactjs typescript mobx mobx-react