typedash
Version:
modern, type-safe collection of utility functions
23 lines (19 loc) • 872 B
text/typescript
import { K as KeysOfUnion } from './KeysOfUnion-BrkZWXzm.cjs';
/**
* A utility type around `string` that allows for autocomplete on the string on a specific set of values, yet accepting any string.
* @see https://twitter.com/diegohaz/status/1524257274012876801
*/
type StringWithAutocomplete<S extends string> = S | (string & Record<never, never>);
/**
* Returns whether the input value has the specified key.
* @param value The value to check.
* @param key The key to check for.
* @returns Whether the input value has the specified key.
* @example
* ```ts
* hasKey({ a: 1 }, 'a') // true
* hasKey({ a: 1 }, 'b') // false
* ```
*/
declare function hasKey<T extends object, K extends StringWithAutocomplete<KeysOfUnion<T> & string> | PropertyKey>(value: T, key: K): value is T & Record<K, unknown>;
export { type StringWithAutocomplete as S, hasKey as h };