isValueOf
Checks whether a value exists among the values of a given object. This helper works with any plain object, including objects with a null prototype.
It never throws an error and never mutates the input. Use it when you need to confirm that a value is one of the values of a particular object.
Signature
function isValueOf<T extends Record<PropertyKey, unknown>>(
obj: unknown,
value: unknown
): obj is T
Parameters
| Name | Data type | Description |
|---|---|---|
| object | T |
The object to check keys against. Must be a non-null object. |
| key | unknown |
The value to check as a potential key. |
Returns
Boolean:
true: If value exists among the values of obj, false otherwise.
false: If obj is null or a primitive.
Works for any plain object, including objects created with Object.create(null).
Behavior
- The function performs an exact equality check (===) against each value in the object.
- Never throws and never mutates input.
- Suitable for strict type narrowing: after if (isValueOf(obj, value)) { … }, TypeScript knows obj is of type T.
Examples
const obj = { a: 1, b: 2, c: "hello" };
isValueOf(obj, 1); // true
isValueOf(obj, "hello"); // true
const nullProto = Object.create(null);
nullProto.x = 123;
isValueOf(nullProto, 123); // true
isValueOf(obj, 3); // false
isValueOf(null, 1); // false
isValueOf(42, 1); // false
Notes
- Does not coerce types.
"1"will not match 1, and boxed primitives(e.g., Object(1))are treated as objects, not numbers. - Works correctly with empty objects (
{}) and null-prototype objects. - Designed for pure, functional usage: it never mutates the input object and never throws exceptions.