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<?T>` to `$ReadOnlyArray<?T | number>`
in the declaration of `filterList` caused no Flow error prior to this
commit, but now causes one.

wchargin-branch: filter-list-typecheck
This commit is contained in:
William Chargin 2019-08-26 10:35:08 -07:00 committed by GitHub
parent 909045a7ec
commit ae8ab0d1bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 1 deletions

View File

@ -89,5 +89,8 @@ export function orElse<T>(x: ?T, defaultValue: T): T {
* in a type-aware way.
*/
export function filterList<T>(xs: $ReadOnlyArray<?T>): 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[]);
}