Ramana
Ramana

Reputation: 1935

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

.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 ??

Upvotes: 4

Views: 6507

Answers (2)

Kirk Larkin
Kirk Larkin

Reputation: 93053

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);

Upvotes: 2

Dalorzo
Dalorzo

Reputation: 20014

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);

Upvotes: 2

Related Questions