-
-
Notifications
You must be signed in to change notification settings - Fork 0
has methods
Nicholas Berlette edited this page Jun 19, 2025
·
1 revision
function hasMethods<T, K extends readonly PropertyKey[]>(it: T, ...keys: K): it is Extract<T, {
[P in [object Object]]:
P extends keyof T ? T[P] extends Fn ? T[P] : never : Fn
}>;
Checks if the given value contains methods for all of the specified keys. No other properties or traits are checked by this guard, such that the target value may be even be a primitive value like a string or number.
Name | Info |
---|---|
it |
The object to check. |
keys |
The keys to check for. |
true
if the object has all the specified keys, and each key is a callable
method; otherwise, false
.
Guards
import { hasMethods } from "@nick/is/has-methods";
const obj = { foo: () => "foo", bar: () => "bar", baz: 42 };
console.assert(hasMethods(obj, "foo", "bar")); // OK
import { hasMethods } from "@nick/is/has-methods";
const ab = new ArrayBuffer(8);
console.assert(hasMethods(ab, "slice")); // OK
// checking if resizable array buffer methods are available
if (hasMethods(ab, "resize", "transfer")) {
console.log("Great news - the RAB proposal is supported!");
}
function hasMethods<K extends readonly PropertyKey[]>(
it: unknown,
...keys: K
): it is {
[P in undefined]: Fn;
};
function hasMethods(it: unknown, ...keys: PropertyKey[]): boolean;
function hasMethods(it: unknown, ...keys: PropertyKey[]): boolean;