[英]TypeScript dynamic property filtering mobx observable list
I have a calendar application I am building in React using Typescript.我有一个使用 Typescript 在 React 中构建的日历应用程序。 I am currently trying to store all my calendar events in a single observable object and select an array of events based on date
我目前正在尝试将我所有的日历事件存储在一个可观察的 object 和 select 基于日期的事件数组中
My events object looks like this我的活动 object 看起来像这样
events = {
'17-08-2020': [Array of events],
'18-08-2020': [{}, {}, {}]
}
The idea behind this structure is that the date key will always be unique so a I can easily just push events into an array that this key identifies.这种结构背后的想法是日期键始终是唯一的,因此我可以轻松地将事件推送到该键标识的数组中。
The problem occurs with typescript restricting me.问题发生在 typescript 限制我。 When I access the store in my component I get this error
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
.当我访问组件中的商店时,我收到此错误
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
。
When I try and use a computed property to get the element in my store I get getter must not have any formal parameters
当我尝试使用计算属性来获取商店中的元素时,我得到
getter must not have any formal parameters
Store店铺
@observable events = {}
@computed
get filteredEvents(key) {
return this.events[key]
}
Just tell typescript that keys can be any string and not only predefined strings that you have on your events object.只需告诉 typescript 键可以是任何字符串,而不仅仅是您在事件 object 中拥有的预定义字符串。
events: {[key: string]: CalendarEventType[]} = {
'17-08-2020': [{}, {}, {}],
'18-08-2020': [{}, {}, {}]
}
For computed values with parameters you can follow the docs: Computeds with arguments对于带有参数的计算值,您可以遵循文档: Computeds with arguments
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.