@wishtack/get
is a TypeScript typed functional alternative to Optional Chaining Operator (a.k.a. Safe Navigation Operator).
yarn add @wishtack/get
or
npm install --save @wishtack/get
import { get } from '@wishtack/get';
interface User {
name: string;
address?: {
postalCode: string;
street: string;
};
}
const user: User = {
name: 'Foo BAR'
};
get(user, 'address', 'postalCode'); // -> undefined
get(user, 'address', 'code'); // -> Compile time error
// playground.ts: - error TS2345: Argument of type '"code"' is not assignable to parameter of type '"postalCode" | "street"'.
//
// console.log(get(user, 'address', 'code'));
// ~~~~~~~~