Add notNull filter with type guard
Summary: You need to use a type guard when narrowing types in a filter. Reviewed By: danielbuechele Differential Revision: D17163782 fbshipit-source-id: aa78bdd392653ebf1080a04e62e131b607e5181b
This commit is contained in:
committed by
Facebook Github Bot
parent
31307c89d7
commit
16457e2adb
12
src/utils/typeUtils.tsx
Normal file
12
src/utils/typeUtils.tsx
Normal file
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* Copyright 2018-present Facebook.
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
* @format
|
||||
*/
|
||||
|
||||
// Typescript doesn't know Array.filter(Boolean) won't contain nulls.
|
||||
// So use Array.filter(notNull) instead.
|
||||
export function notNull<T>(x: T | null | undefined): x is T {
|
||||
return x !== null && x !== undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user