-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathobject-has-in.ts
More file actions
40 lines (31 loc) · 1.25 KB
/
object-has-in.ts
File metadata and controls
40 lines (31 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
Check if an object has a property (including inherited) and narrow the object type.
This function performs __object narrowing__ - it adds the checked property to the object's type, allowing safe property access. Uses the `in` operator to check the entire prototype chain.
Unlike `objectHasOwn` (own properties only) and `keyIn` (key narrowing), this narrows the _object_ type to include inherited properties.
@example
```
import {objectHasIn} from 'ts-extras';
const data: unknown = {foo: 1};
if (objectHasIn(data, 'foo')) {
// `data` is now: unknown & {foo: unknown}
console.log(data.foo); // Safe access
}
// Also checks prototype chain
if (objectHasIn(data, 'toString')) {
// `data` is now: unknown & {toString: unknown}
console.log(data.toString); // Safe access to inherited method
}
```
@note This uses the `in` operator and checks the entire prototype chain, but blocks `__proto__` and `constructor` for security.
@category Type guard
*/
export function objectHasIn<ObjectType, Key extends PropertyKey>(
object: ObjectType,
key: Key,
): object is (ObjectType & Record<Key, unknown>) {
// Guard against prototype pollution
if (key === '__proto__' || key === 'constructor') {
return false;
}
return key in (object as Record<PropertyKey, unknown>);
}