Is it possible to have a conidial required argument type based on the first argument type:
type ConditionalRequiredArg<T, P> = T extends string ? P | undefined : P;
function func<T, P>(_arg1: string | number, _arg2: ConditionalRequiredArg<T, P>) {}
func('foo'); // Error: Expected 2 arguments, but got 1.
In theory, the above function should not require a second argument, but it does!
Edit: I am aware of '?' for optional argument types, but I would like to make it conditional to leverage the compiler error and suggestions.
You can always use the signature overloading to screen users from directly using the implementation signature:
function func<P>(_arg1: string, _arg2?: P)
function func<P>(_arg1: number, _arg2: P)
function func<P>(_arg1: string | number, _arg2?: P) { }
This code forces user to choose either the first signature or the second to use, and not the third signature of the implementation.
func('foo', 1); // success
func('foo'); // success
func(1, 1); // success
func(1); // Error: Argument of type 'number' is not assignable to parameter of type 'string'
I think you are mixing up two different things.
func<T, P>(_arg1: string | number, _arg2: ConditionalRequiredArg<T, P>)
means that the function expects 2 arguments, where the second one might have the type undefined, meaning:
_arg2 = undefined
, where the field _arg2
is already declared.
But you want the field to be not declared.
_arg2 = undefined
is not equivalent of saying:
func<T, P>(_arg1: string | number, undefined)
For this use case, you can use ? as follows:
func<T, P>(_arg1: string | number, _arg2?: ConditionalRequiredArg<T, P>)
Edit:
Types and arguments are two different things. The type defines the type of the value, whilst an argument is a field.
Although it is not a good practice, you could also initialize the field manually with undefined as follows:
func<T, P>(_arg1: string | number, _arg2: string = undefined)
But this is in the end same as _arg2?
The type undefined
is like saying "The field does not contain anything" or better say, "The field contains undefined", whilst the field undefined
is "The field does not exist"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With