Skip to main content

prop

prop() function

Get the value of a property from an object, or return undefined if the property does not exist on the object.

Signature:
declare function prop<K extends Key, T>(key: K, obj: T): Prop<T, K>;

declare function prop<K extends Key>(key: K): <T>(obj: T) => Prop<T, K>;

Example

// get the `name` property from an object
const person = { name: "John", age: 30 };
const name = prop("name", person); // "John"

// with pipe
pipe(
person,
prop("name"),
);

// get the `address` property from an object that may be null or undefined
const maybePerson = null;
const address = prop("address", maybePerson); // undefined

Open Source Code