Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix(nocodb-sdk): update ncHasProperties fn to support primitive key

Ramesh Mane81f3b0e54743
diff --git a/packages/nocodb-sdk/src/lib/is.ts b/packages/nocodb-sdk/src/lib/is.ts
index 3277e3ac3d..7f4f134130 100644
--- a/packages/nocodb-sdk/src/lib/is.ts
+++ b/packages/nocodb-sdk/src/lib/is.ts
@@ -33,9 +33,12 @@ export function ncIsEmptyObject(value: any): boolean {
/**
* Checks whether the given value is an object and contains all the specified properties.
*
+ * Supports checking either a **single property** or **multiple properties** at once.
+ *
* @template T - The expected object type.
* @param value - The value to check.
- * @param keys - An array of property keys that should exist in the object.
+ * @param keyOrKeys - A property key or an array of property keys that should exist in the object.
+ * Can be a single string, a single `keyof T`, an array of strings, or an array of `keyof T`.
* @returns {value is T} - Returns `true` if `value` is an object containing all specified keys, otherwise `false`.
*
* @example
@@ -44,28 +47,60 @@ export function ncIsEmptyObject(value: any): boolean {
*
* const obj = { name: "Alice", age: 25 };
*
+ * // Check multiple keys
* if (ncHasProperties<User>(obj, ["name", "age"])) {
* console.log(obj.name); // βœ… TypeScript ensures obj.name is safe to access
* }
+ *
+ * // Check single key
+ * if (ncHasProperties<User>(obj, "age")) {
+ * console.log(obj.age); // βœ… Safe to access
+ * }
* ```
*
* @example
* ```typescript
* const obj = { title: "Hello", value: "World" };
*
+ * // Using string array
* if (ncHasProperties(obj, ["title", "value"])) {
* console.log(obj["title"]); // βœ… Safe to access without explicit type
* }
+ *
+ * // Using single string
+ * if (ncHasProperties(obj, "title")) {
+ * console.log(obj.title); // βœ… Works
+ * }
* ```
*/
-export function ncHasProperties<T extends object>(
+export function ncHasProperties<T extends object = object>(
+ value: any,
+ key: keyof T
+): value is T;
+export function ncHasProperties<T extends object = object>(
+ value: any,
+ key: string | number | symbol
+): value is T;
+export function ncHasProperties<T extends object = object>(
value: any,
keys: readonly (keyof T)[]
): value is T;
export function ncHasProperties<T extends object = object>(
value: any,
- keys: readonly string[]
+ keys: readonly (string | number | symbol)[]
+): value is T;
+// Implementation signature (broad enough)
+export function ncHasProperties<T extends object = object>(
+ value: any,
+ keyOrKeys:
+ | string
+ | number
+ | symbol
+ | keyof T
+ | readonly (string | number | symbol)[]
+ | readonly (keyof T)[]
): value is T {
+ const keys = ncIsArray(keyOrKeys) ? keyOrKeys : [keyOrKeys];
return ncIsObject(value) && keys.every((key) => key in value);
}