I’m trying to make a value resolver like this:
type Method<TValue, TArgs extends [...any]> = (...args: [...TArgs]) => TValue;
function getValue<TValue, TArgs extends [...any]>(value: TValue | Method<TValue, TArgs>, args?: typeof value extends Method<TValue, TArgs> ? TArgs : never): TValue | undefined {
if (typeof value === 'function') {
return args ? (value as Method<TValue, TArgs>)(...args) : undefined;
}
return value;
}
But when I try to call it with a function, like so:
function test(num: number): number { return num };
const value1 = getValue(test, [23]);
^^-ERROR
const value2 = getValue<number, [number]>(test, [23]);
^^-ERROR
I get this error:
Argument of type ‘number[]’ is not assignable to parameter of type ‘undefined’
Does anyone know how I can get the args
parameter to correctly resolve to the required array of parameters?