类型'Observable <boolean | “>”不可分配给类型“ Observable <boolean>” TS2322

时间:2019-09-03 14:56:34

标签: javascript angular typescript ngrx

我正在从NgRx商店@ngrx/store": "7.3.0"中选择。

// typeof state.id == string

export const selectIsSomethingPresent = createSelector(selector, state => {
  // standard null check
  return state.id && state.id !== "12345";
});

我将它分配给一个可观察对象

isSomethingPresent$ !: Observable<boolean>;

someMethod() {
  this.isSomethingPresent$ = this.store.select(selectIsSomethingPresent);
}

我收到此错误:

Type 'Observable<boolean | "">' is not assignable to type 'Observable<boolean>'.

打字稿为什么会这样?

fyi return ("12345" && true)会引发相同的错误。

1 个答案:

答案 0 :(得分:2)

由于4 + 1 = 5类型为def breakingRecords(scores): length_score = len(scores) - 1 c = 0 z = 0 for i in range(length_score): q = scores[i + 1] if scores[i] < q: c += 1 elif scores[i] > q: z += 1 else: z = 0 return [c, z] ,空字符串&& true将返回空字符串而不是布尔值,因此您需要将state.id转换为string

state.id