source: node_modules/es-toolkit/dist/compat/object/has.d.mts

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.7 KB
RevLine 
[a762898]1import { PropertyPath } from '../_internal/PropertyPath.mjs';
2
3/**
4 * Checks if a given path exists within an object.
5 *
6 * @template T
7 * @template K
8 * @param {T} object - The object to query.
9 * @param {K} path - The path to check.
10 * @returns {object is T & { [P in K]: P extends keyof T ? T[P] : Record<string, unknown> extends T ? T[keyof T] : unknown } & { [key: symbol]: unknown }} Returns a type guard indicating if the path exists in the object.
11 *
12 * @example
13 * const obj = { a: 1, b: { c: 2 } };
14 *
15 * if (has(obj, 'a')) {
16 * console.log(obj.a); // TypeScript knows obj.a exists
17 * }
18 *
19 * if (has(obj, 'b')) {
20 * console.log(obj.b.c); // TypeScript knows obj.b exists
21 * }
22 */
23declare function has<T, K extends PropertyKey>(object: T, path: K): object is T & {
24 [P in K]: P extends keyof T ? T[P] : Record<string, unknown> extends T ? T[keyof T] : unknown;
25} & {
26 [key: symbol]: unknown;
27};
28/**
29 * Checks if a given path exists within an object.
30 *
31 * @template T
32 * @param {T} object - The object to query.
33 * @param {PropertyPath} path - The path to check. This can be a single property key,
34 * an array of property keys, or a string representing a deep path.
35 * @returns {boolean} Returns `true` if the path exists in the object, `false` otherwise.
36 *
37 * @example
38 * const obj = { a: { b: { c: 3 } } };
39 *
40 * has(obj, 'a'); // true
41 * has(obj, ['a', 'b']); // true
42 * has(obj, ['a', 'b', 'c']); // true
43 * has(obj, 'a.b.c'); // true
44 * has(obj, 'a.b.d'); // false
45 * has(obj, ['a', 'b', 'c', 'd']); // false
46 * has([], 0); // false
47 * has([1, 2, 3], 2); // true
48 * has([1, 2, 3], 5); // false
49 */
50declare function has<T>(object: T, path: PropertyPath): boolean;
51
52export { has };
Note: See TracBrowser for help on using the repository browser.