How fix bind() function after signature function?

Viewed 24

When I try to run this code:

return console[methodName as keyof Console].bind(console, "[" + PREFIX + "]", "[" + methodNameDisplay + "]");

I have error:

This expression is not callable.
  Each member of the union type '{ <T>(this: T, thisArg: ThisParameterType<T>): OmitThisParameter<T>; <T, A0, A extends any[], R>(this: (this: T, arg0: A0, ...args: A) => R, thisArg: T, arg0: A0): (...args: A) => R; <T, A0, A1, A extends any[], R>(this: (this: T, arg0: A0, arg1: A1, ...args: A) => R, thisArg: T, arg0: A0, arg1: A1): (...args: A) =>...' has signatures, but none of those signatures are compatible with each other.t

I think the problem with the bind function after console[methodName as keyof Console]. How to fix this problem? In js it works.

1 Answers

IMHO it's better to avoid .bind in typescript, use arrow function instead. Here is an example:

function log(methodName: Extract<keyof Console, 'log' | 'error'>) {
    return (methodNameDisplay: string) => {
        console[methodName](methodNameDisplay);
    }
}
Related