UNPKG

typedash

Version:

modern, type-safe collection of utility functions

1 lines 895 B
{"version":3,"file":"hasKey-Bt_UpAcG.cjs","names":[],"sources":["../src/functions/hasKey/hasKey.ts"],"sourcesContent":["import type { KeysOfUnion, StringWithAutocomplete } from '../../types';\n\n/**\n * Returns whether the input value has the specified key.\n * @param value The value to check.\n * @param key The key to check for.\n * @returns Whether the input value has the specified key.\n * @example\n * ```ts\n * hasKey({ a: 1 }, 'a') // true\n * hasKey({ a: 1 }, 'b') // false\n * ```\n */\nexport function hasKey<\n T extends object,\n K extends StringWithAutocomplete<KeysOfUnion<T> & string> | PropertyKey,\n>(value: T, key: K): value is T & Record<K, unknown> {\n if (typeof value !== 'object' || value == null) {\n return false;\n }\n\n return key in value;\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,SAAgB,OAGd,OAAU,KAAyC;AACnD,KAAI,OAAO,UAAU,YAAY,SAAS,KACxC,QAAO;AAGT,QAAO,OAAO"}