Objects.hasFunction()
Syntax
function hasFunction(source: unknown, functionName: string | number | symbol, argumentCount?: number): boolean
Checks if an object contains a function by the specified name and number of arguments.
Parameters | |
source
|
The value to check for presence of the function. Accepts any type. |
functionName
|
The name of the function to check for. |
argumentCount
Optional
|
If passed, |
Example
import { Objects } from 'potence';
const object: unknown = { ... };
if (Objects.hasFunction(object, 'doSomething', 2)) {
object.doSomething('with', 'me'); // no error
}
Remarks
This function is a
type guard.
When used in an if
condition, this function will therefore allow you to call
source[functionName]()
without any TypeScript errors.
Note that, because it is not possible to perform a runtime check for the type of
parameters or the return type, both of these will be typed as unknown
. In
other words: you will be able to pass an arbitrary number of arguments of any
type to the function, but to use the function’s return type you’ll have to
perform another type check.
This function is particularly useful when defining custom type guards.
Compare with Objects.hasProperty()
,
which can check for the presence of properties of other types.
Note: For the sake of simplicity, the above function signature is an abridged version of the function’s real signature. To ensure proper TypeScript typing—especially in regards to the return type—a slightly more complex function signature is needed than what is shown here.