When working with TypeScript, I am facing an issue. I have an array defined as Array<string|undefined
, and my goal is to filter out the undefined values from this array and assign the resulting array to a variable of type Array<string>
. However, I am encountering a type error. How can I solve this problem?
For example:
const a: Array<string|undefined> = ['a', undefined, 'b'];
const b: Array<string> = a.filter(Boolean); // ERROR
The error message states:
Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string'.