-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
Bug Report
π Search Terms
type predicate filter flatMap null
π Version & Regression Information
This is the behavior in every version I tried (that don't throw errors), and I reviewed all FAQ entries.
β― Playground Link
Playground link with relevant code
π» Code
import { F } from "ts-toolbelt"; // can easily be replaced with raw TS code if needed
const satisfies =
<TWide,>() =>
<TNarrow extends TWide>(narrow: F.Narrow<TNarrow>) =>
narrow;
const isNotNull = <T,>(value: T | null): value is T => value !== null;
type Item = Array<{ value: string | null }>;
const item1 = satisfies<Item>()([{ value: "1" }]);
const item2 = satisfies<Item>()([{ value: "2" }]);
const item3 = satisfies<Item>()([{ value: null }]);
const values1 = [item1, item2, item3].flatMap(item => item.map(p => p.value));
const filteredValues1 = values1.filter(isNotNull); // still contains `null` in type :(
const values2: Array<"1" | "2" | null> = ["1", "2", null]; // same reported type as `values`
const filteredValues2 = values2.filter(isNotNull); // `null` correctly gets excluded from typeπ Actual behavior
filteredValues1 still contains null in its type when is shouldn't.
π Expected behavior
filteredValues1 should have the same type as filteredValues2 (without null).
Originated from this Twitter thread: https://twitter.com/joealden_/status/1536335566891061249
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue