API reference / @evolu/common / Object / getProperty

Function: getProperty()

function getProperty<K, V>(record, key): V | undefined;

Defined in: packages/common/src/Object.ts:108

Safely gets a property from a record, returning undefined if the key doesn't exist.

TypeScript's Record<K, V> type assumes all keys exist, but at runtime accessing a non-existent key returns undefined. This helper provides proper typing for that case without needing a type assertion.

Example

const users: Record<string, User> = { alice: { name: "Alice" } };
const user = getProperty(users, "bob"); // User | undefined

Type Parameters

Type Parameter
K extends string
V

Parameters

ParameterType
recordReadonlyRecord<K, V>
keystring

Returns

V | undefined

Was this page helpful?