export const isFunction = (obj: unknown): obj is Function => obj instanceof Function;
export const isString = (obj: unknown): obj is string => Object.prototype.toString.call(obj) === "[object String]";
我想写 isFunction 方法 - 类似于 isString,但是 typescript/eslint 给了我一个错误:
Don't use `Function` as a type. The `Function` type accepts any function-like value.
It provides no type safety when calling the function, which can be a common source of bugs.
It also accepts things like class declarations, which will throw at runtime as they will not be called with `new`.
If you are expecting the function to accept certain arguments, you should explicitly define the function shape @typescript-eslint/ban-types
有没有办法做到这一点?
PS这是答案:
export const isFunction = (obj: unknown): obj is (...args: any[]) => any => obj instanceof Function;