Here's a challenge that I'm facing. I have this specific type definition:
type FuncType<T> = (value: T) => T
I want to create a function using this type that follows this structure:
const myFunc: FuncType<T> = (value) => value;
I intend to use it in the following manner:
const a: string = myFunc<string>('a');
const b: number = myFunc<number>(2);
However, the line
const myFunc: FuncType<T> = (value) => value;
is not syntactically correct.
How can I implement this correctly?
Note: I managed to find a workaround by introducing an extra function, even though I want to steer clear of unnecessary currying (currying isn't allowed in my actual situation since it's related to react hooks):
const myFunc = <T>(): FuncType<T> => (value) => value;
const a: string = myFunc<string>()('a');
const b: number = myFunc<number>()(2);
Why do I have to use this type alias instead of writing it directly?
const myFunc = <T>(value: T): T => value;
The reason behind this is that my real function type definition is more complex than a straightforward one.
It looks something like this:
interface FuncType<T> {
(args: {arg1: T}): {res1: T}
(args: {arg1: T, arg2: T}): {res1: T, res2: T}
}