I was wondering if there is a way to declare a generic class with default generic type which:
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:
class ClassA<MyGenericType = never> {
public methodWithGenericArgument(...args: MyGenericType extends never ? [] : [MyGenericType]): void {
// Do smth
}
}
type NotOptionalArgumentType = number;
const instanceX = new ClassA();
instanceX.methodWithGenericArgument(); // OK
instanceX.methodWithGenericArgument(15); // ERROR
const instanceY = new ClassA<NotOptionalArgumentType>();
instanceY.methodWithGenericArgument(); // ERROR
instanceY.methodWithGenericArgument(15); // OK
let argumentValue!: NotOptionalArgumentType;
const instanceZ = new ClassA<NotOptionalArgumentType>();
instanceZ.methodWithGenericArgument(); // ERROR
instanceZ.methodWithGenericArgument(argumentValue); // OK