My situation involves a higher order function called `createHandler`, which takes rest arguments in a variadic mapped tuple format and maps them to a generic object type (let's call it `ObjA<>`). The function returned can then be passed to another higher order function, `useHandler`, which will call it (performing additional actions).
I want to ensure that the function returned by `createHandler` is of a specific type (intersected with a "brand", i.e. `BrandedValidHandler`), so that only `useHandler` is able to accept and execute it.
I am aiming to validate the input arguments array to `createHandler` before considering it as valid input. Specifically, I need to check for duplicate strings in a field of `ObjA<>` and reject the input if duplicates are found. However, I'm facing challenges in executing this duplication check on the argument array without losing the generic type inference, especially with contextual typing.
Is there a way to perform this validation on the inputs without sacrificing the inference?
One solution I have considered is validating the inputs within the return type of `createHandler` instead, returning an error type rather than `BrandedValidHandler`. While this approach technically works since `createHandler` and `useHandler` are expected to be used together, ideally the invalid inputs themselves would be detected instead of just affecting the return type. Here is a simplified example:
// Code example here
Instead of moving the validation to the return type, I would like to implement the duplicate check within the mapped type created in `MapUsers`. However, attempting this appears to result in loss of generic inference:
// Another code snippet
In my attempts, I've tried different approaches but faced challenges such as losing contextual typing or recognition of arrays. I'm seeking a method to preserve the generics inferred in the `infer users` line, which seems challenging due to the nature of the `User` type. This scenario extends from my previous questions on Stack Overflow (link), and draws inspiration from existing solutions for checking array uniqueness (link).