Add getOwn utility function

This commit is contained in:
Evan Hahn 2020-11-04 13:56:49 -06:00 committed by GitHub
parent d1c63609a7
commit ca83281986
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 10 deletions

13
ts/util/getOwn.ts Normal file
View file

@ -0,0 +1,13 @@
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { has } from 'lodash';
// We want this to work with any object, so we allow `object` here.
// eslint-disable-next-line @typescript-eslint/ban-types
export function getOwn<TObject extends object, TKey extends keyof TObject>(
obj: TObject,
key: TKey
): TObject[TKey] | undefined {
return has(obj, key) ? obj[key] : undefined;
}