[英]TypeScript dynamic property filtering mobx observable list
我有一個使用 Typescript 在 React 中構建的日歷應用程序。 我目前正在嘗試將我所有的日歷事件存儲在一個可觀察的 object 和 select 基於日期的事件數組中
我的活動 object 看起來像這樣
events = {
'17-08-2020': [Array of events],
'18-08-2020': [{}, {}, {}]
}
這種結構背后的想法是日期鍵始終是唯一的,因此我可以輕松地將事件推送到該鍵標識的數組中。
問題發生在 typescript 限制我。 當我訪問組件中的商店時,我收到此錯誤Element implicitly has an 'any' type because expression of type 'string' can't be used to index type
。
當我嘗試使用計算屬性來獲取商店中的元素時,我得到getter must not have any formal parameters
店鋪
@observable events = {}
@computed
get filteredEvents(key) {
return this.events[key]
}
只需告訴 typescript 鍵可以是任何字符串,而不僅僅是您在事件 object 中擁有的預定義字符串。
events: {[key: string]: CalendarEventType[]} = {
'17-08-2020': [{}, {}, {}],
'18-08-2020': [{}, {}, {}]
}
對於帶有參數的計算值,您可以遵循文檔: Computeds with arguments
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.