Here is a code snippet (TS playground link):
const enum Enum { A, B, C }
interface Args {
e: Enum.A;
}
interface GenericClass<A> {
new (args: A) : void;
}
class TestClass {
constructor(args: Args) {}
}
function func<A>(C: GenericClass<A>, args: A) {
return new C(args);
}
func(TestClass, { e: Enum.A });
The final line [1]
triggers an error when strictFunctionTypes
is enabled:
Argument of type 'typeof TestClass' is not assignable to parameter of type 'GenericClass<{ e: Enum; }>'.
Types of parameters 'args' and 'args' are incompatible.
Type '{ e: Enum; }' is not assignable to type 'Args'.
Types of property 'e' are incompatible.
Type 'Enum' is not assignable to type 'Enum.A'.
This is perplexing as I am accepting the exact enum value Enum.A
and passing the same value Enum.A
into the function.
I understand that I can use type casting like { e: <Enum.A>Enum.A }
, but it seems awkward. Is there a way to resolve this issue without resorting to type casting?