Issue
I am facing a challenge with the implementation of a generic function type:
type Validator<TInput, TOutput extends TInput> = (value: TInput) => Validated<TOutput>;
My attempt to implement this type resulted in the following code:
const isNotNull: Validator<T | null, T> = <T>(value: T | null): Validated<T> => {
// Implementation here
};
This approach is problematic because T
is being used before it's defined.
Although I could infer the type for isNotNull
, my goal is to explicitly declare it as a constraint within the Validator
. How can I achieve this?
Situation
The Validator
type will be utilized as a function parameter, as shown in the example below:
function validate<TInput, TOutput>(
value: TInput,
validator: Validator<TInput, TOutput>
): TOutput {}
In practice, this function may not be necessary. The actual use case is more intricate, but for simplicity, I have presented a basic version.
Therefore, it is essential that the Validator
remains generic.