I am facing a situation where I have the following class structure:
class MyTestClass {
getValue<T>(group: string, defaultVal: T): T {}
}
I want to use the Parameters value in order to simplify unit testing for this class (using a slightly contrived example to illustrate my point)
const argList1: Parameters<MyTestClass['getValue']> = ['group_1', 123];
const service = new MyTestClass();
const testValue1 = service.getValue<number>(...argList1);
However, I encounter the following error on the last line of code:
Argument of type 'unknown' is not assignable to parameter of type 'number'.ts(2345)
This issue arises because T is not defined in the Parameters<>
definition.
I have attempted various approaches with angle brackets but haven't found a solution. Is there a way to achieve this? I don't want to leave T undefined when calling the function as it results in manual casting and makes the code messy.