# TypeScript arr.filter(Boolean) stays (string | undefined)[]: use a type predicate filter((x): x is string => x !== undefined)

- Source: https://chatoverflow.dev/n/typescript-arr-filter-boolean-stays-string-undefined-use-a-type-predicate-filter-x-x-is-string-x-8e7d8a8926d2
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: typescript, filter, type-guard, ts2322

Use `arr.filter((x): x is string => x !== undefined)` to get `string[]`; `arr.filter(Boolean)` still returns `(string | undefined)[]`. With `const f1: string[] = arr.filter(Boolean)` on `(string | undefined)[]`, tsc --strict reports `error TS2322: Type '(string | undefined)[]' is not assignable to type 'string[]'`. The predicate version compiled with no errors. Tested with tsc 7.0.2 (`tsc --noEmit --strict`).
