2017-01-23 4 views
1

は、私は私の効果/店でこのコードを持っている:なぜthis.store.select( "uiState")ngrx /ストアに空のオブジェクトを返す(2.0+)

@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' does not exist on type '{}'.

そしてここでは、私の店ファイルです:

 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 
     } 

誰でも知っていますか?

UPDATE:

@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 
     })) 

Argument of type '(uiState: UiState) => string' is not assignable to parameter of type '(value: UiState, index: number) => boolean'. Type 'string' is not assignable to type 'boolean'.)

私は方法が必要ですFirebaseの呼び出しに未定義またはnullを渡さないように、初期状態または空のuserId状況をフィルタリングします。

答えて

2

問題は、このコードのビットである:

this.store.select("uiState") 

selectオペレータは、店舗の状態からという名前のプロパティを抽出し、その値を発します。しかし、TypeScriptにはそのプロパティの型を推論する方法がありません。問題を解決するには、type variableを経由して、明示的に型を指定することができます

this.store.select<UiState>("uiState") 

をあなたの改正問題で、この行について:

.filter(uiState => uiState.userId) 

filter operatorbooleanとあなたを返すことになっている述語を取りますuserIdstring)が返されます。そこには暗黙の活字体の変換はありませんので、あなたは明示的にする必要があります。

.filter(uiState => Boolean(uiState.userId)) 
+0

(「uiState」)は、私はまだtypescriptですエラーに遭遇しthis.store.selectあなたのコードを使用してください。なぜ(typescript noobでも)わからない。更新された質問をご覧ください。再度、感謝します! –

+1

'userId'は' string'で、 'filter'は' boolean'の結果を得たいので明示的に '.filter(uiState => Boolean(uiState.userId))'にする必要があります – cartant

+0

フィルタコードは.filter(uiState => {if(uiState.userId){true true;} else {false false}})?私は人々が.filter(uiState => !! uiState.userId)と書いているのを見ました - それは同じことです - userIdが空でないなら、続行してください。それ以外のブレーク操作ですか? –

関連する問題