I was wondering if there is a way to declare a generic class with default generic type which:
- By default would allow calling a method of the class without passing arguments
- If another generic type is defined, then the method would be able to be called only when passing an argument of the generic type.
Pseudocode
class ClassA<MyGenericType = OptionalArgumentType> {
public methodWithGenericArgument(argumentA: MyGenericType): void {
// Do smth
}
}
//
const instanceX = new ClassA();
instanceX.methodWithGenericArgument(); // CORRECT! We use default optional argument type
//
const instanceY = new ClassA<NotOptionalArgumentType>();
instanceY.methodWithGenericArgument(); // ERROR! Compiler should throw an error here, because we defined NOT OPTIONAL type
//
const argumentValue: NotOptionalArgumentType;
const instanceZ = new ClassA<NotOptionalArgumentType>();
instanceZ.methodWithGenericArgument(argumentValue); // CORRECT! We pass argument with required value
With some trickery, you can make this work. The recipe includes:
never
as the default type for the generic type parameterThe idea is to conditionally switch between an empty tuple and a tuple of one argument (or more, or several conditions - go wild depending on your implementation). Here is how this would look like:
Playground