Problem
I have the following generic function type:
type Validator<TInput, TOutput extends TInput> = (value: TInput) => Validated<TOutput>;
Now I want to implement this type, so I did the following:
const isNotNull: Validator<T | null, T> = <T>(value: T | null): Validated<T> => {
// Implementation here
};
This doesn't work because T is used before it's defined.
I know I can infer the type of isNotNull but I want to explicitly declare it as Validator as a constraint. How do I do this?
Context
This Validator type will be used as a function argument, like the following:
function validate<TInput, TOutput>(
value: TInput,
validator: Validator<TInput, TOutput>
): TOutput {}
Of course in reality there is no need for this function. The real scenario is more complex, but I've stripped it down to the minimum.
This is why Validator must be generic.