From ae8ab0d1bd15036537d160f8c0c956a17d201ea4 Mon Sep 17 00:00:00 2001 From: William Chargin Date: Mon, 26 Aug 2019 10:35:08 -0700 Subject: [PATCH] Check typesafety of `NullUtil.filterList` (#1328) Summary: The current implementation of `NullUtil.filterList` uses an `any`-cast. This is fine as long as the definition is actually typesafe; we should take a least a little care to ensure that it is. This commit adds a typesafe version, commented out but still typechecked, and refines the type around the `any`-cast to make the cast slightly more robust. Test Plan: Note that changing `$ReadOnlyArray` to `$ReadOnlyArray` in the declaration of `filterList` caused no Flow error prior to this commit, but now causes one. wchargin-branch: filter-list-typecheck --- src/util/null.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/util/null.js b/src/util/null.js index 9150a93..9736fe1 100644 --- a/src/util/null.js +++ b/src/util/null.js @@ -89,5 +89,8 @@ export function orElse(x: ?T, defaultValue: T): T { * in a type-aware way. */ export function filterList(xs: $ReadOnlyArray): T[] { - return (xs.filter((x) => x != null): any); + // A type-safe way to implement this would be: + /*:: (xs.flatMap((x) => x == null ? [] : [x]): T[]); */ + // For performance, we instead take an unsafe route. + return ((xs.filter((x) => x != null): any): T[]); }