Imagine a scenario where the following code is present:
type QueryResult<ResultType = string, ErrorType = string> = {
result: ResultType,
} | {
errors: ErrorType,
}
So, if I want to initialize my result, I can proceed like this:
const myResult: QueryResult = {
result: "My result!",
};
In addition, when I need to modify the ResultType
, it can be included in the generic type:
const myResult: QueryResult<number> = {
result: 10,
errors: null
};
However, what about changing the type value for errors
? One way of achieving this is by doing the following:
// Although functional, specifying the default value is not ideal
const myErrorResult: QueryResult<string, MyErrorType> = {
errors: {
location: [1, 2],
message: "Invalid argument!",
}
};
Is there a way to skip specifying the ResultType
? Is this even feasible? The main motive behind this query is to avoid providing default values for each generic type, especially when dealing with numerous generics. Attempts such as QueryResult<, string>
and
QueryResult<ErrorType = MyErrorType>
have resulted in errors.
You can access the playground link here.
Thanks for your assistance!
NOTE: This example serves solely for illustrative purposes, acknowledging that it may not be an optimal representation method for errors/results.