ts-extras
Version:
Essential utilities for TypeScript projects
44 lines (34 loc) • 1.4 kB
JavaScript
/**
Check if a key exists in an object and narrow the key type.
This function performs __key narrowing__ - it narrows the key variable to only keys that actually exist in the object. Uses the `in` operator to check the entire prototype chain.
When `keyIn` returns `true`, the key is narrowed to keys that exist in the object.
When it returns `false`, the key type remains unchanged.
Unlike `objectHasIn` and `objectHasOwn` (both do object narrowing), this narrows the _key_ type, making it useful for validating union types of possible keys.
```
import {keyIn} from 'ts-extras';
const object = {foo: 1, bar: 2};
const key = 'foo' as 'foo' | 'bar' | 'baz';
if (keyIn(object, key)) {
// `key` is now: 'foo' | 'bar' (narrowed from union)
console.log(object[key]); // Safe access
} else {
// `key` remains: 'foo' | 'bar' | 'baz' (unchanged)
}
// Works with symbols
const symbol = Symbol.for('myKey');
const objectWithSymbol = {[symbol]: 'value'};
if (keyIn(objectWithSymbol, symbol)) {
// symbol is narrowed to existing symbol keys
}
```
*/
export function keyIn(object, key) {
// Guard against prototype pollution
if (key === '__proto__' || key === 'constructor') {
return false;
}
return key in object;
}