Type 'unknown' is not assignable to type 'string', when using filter(Boolean) operator

Viewed 6280

.ts:

siteName: string;

    this.store.pipe(
          select(getSiteName),
          filter(Boolean),
          take(1)
        ).subscribe(siteName => this.siteName = siteName);

Error:

Type 'unknown' is not assignable to type 'string'

I'm getting the above error for this.siteName, after adding the filter(Boolean) operator to the pipe. Without the filter operator I didn't see any errors, am I missing something ??

2 Answers

I think the error is referring to your parameter of the function subscribe which does not have any type

.subscribe(siteName => this.siteName = siteName);

correct would be:

.subscribe(siteName:string => this.siteName = siteName);

The error occurs because TypeScript doesn't like you using the Boolean constructor as the predicate function passed into filter. It works if you switch over to an explicit predicate function, like this:

this.store.pipe(
    select(getSiteName),
    filter(x => Boolean(x)),
    take(1)
).subscribe(siteName => this.siteName = siteName);
Related