0

I'm selecting from NgRx store @ngrx/store": "7.3.0".

// typeof state.id == string

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

and I'm assigning it to an Observable

isSomethingPresent$ !: Observable<boolean>;

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

I'm getting this error:

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

Why does typescript act like that?

fyi return ("12345" && true) throws same error.

0

1 Answer 1

2

Because state.id type is a string, an empty string && true will return an empty string and not a boolean so you need to convert state.id to boolean

return Boolean(state.id) && state.id !== "12345";
Sign up to request clarification or add additional context in comments.

3 Comments

Them why return "12345" && true throws the same same error?
@KacperCieluch see stackoverflow.com/questions/12693787/…. Note you can use !!state.id instead of Boolean(state.id)
@AndrewAllen IMO it is more readable to use Boolean() then !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.