我的效果/商店中有这个代码:
@Effect() newMessages$ = Observable.interval(5000)
.withLatestFrom(this.store.select("uiState"))
.map(([unreadMessages, uiState]) => new NewMessagesReceivedAction({
unreadMessages,
currentThreadId: uiState.currentThreadId,
currentUserId: uiState.currentUserId
}))
Webstorm警告我:
Property' currentThreadId'类型' {}'。
上不存在
这是我的商店档案:
export interface ApplicationState {
uiState: UiState,
storeData: StoreData
}
export const INITIAL_APPLICATION_STATE: ApplicationState = {
uiState: INITIAL_UI_STATE,
storeData: INITIAL_STORE_DATA
}
这是我的uistate文件:
export interface UiState {
userId: string;
currentThreadId: string;
}
export const INITIAL_UI_STATE: UiState = {
userId: undefined,
currentThreadId: undefined
}
任何人都知道为什么?
更新:
在@cartant建议之后我更新了@Effect()的代码,如下所示,我遇到了另一个Webstorm Typescirpt错误:
@Effect() newMessages$ = Observable.interval(5000)
.withLatestFrom(this.store.select<UiState>("uiState"))
.map(([any,uiState]) => uiState)
.filter(uiState => uiState.userId) //Error right here --- As I need to filter out the uiState.userId when there is no userId when the store initialized with undefined value.
.switchMap(uiState => this.threadsService.loadNewMessagesForUser(uiState.userId))
.withLatestFrom(this.store.select<UiState>("uiState"))
.map(([unreadMessages, uiState]) => new NewMessagesReceivedAction({
unreadMessages,
currentThreadId: uiState.currentThreadId,
currentUserId: uiState.userId
}))
类型的参数&#39;(uiState:UiState)=&gt;字符串&#39;不能分配给&#39;类型的参数(值:UiState,index:number)=&gt;布尔&#39 ;.输入&#39; string&#39;不能分配给&#39; boolean&#39;。)
我需要一种方法来过滤掉初始状态或任何空的userId情况,以确保我没有将未定义或null传入Firebase调用。
答案 0 :(得分:2)
问题在于这段代码:
this.store.select("uiState")
select
运算符从商店的状态中提取命名属性并发出其值。但是,TypeScript无法推断该属性的类型。要解决此问题,您可以通过type variable明确指定类型:
this.store.select<UiState>("uiState")
关于修正问题中的这一行:
.filter(uiState => uiState.userId)
filter
operator采用一个应该返回boolean
的谓词,并且您将返回userId
,这是一个string
。没有隐式TypeScript转换,因此您需要明确:
.filter(uiState => Boolean(uiState.userId))