Let's delve into this concept with an illustrative example!
interface Query {
foo: any;
}
interface Mutation {
bar: any;
}
type OperationName = keyof Query | keyof Mutation;
const request = async <T>(args, operationName: OperationName): Promise<{ data: { [operationName]: T } }> => {
};
The desired outcome is for request
to be called like this:
const result = await request<string>({}, 'foo');
In which case, the value of result
should resemble:
{
data: {
foo: string
}
}
However, an error is encountered stating
A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type. ts(1170)
It seems plausible that this could be achievable since the type OperationName
is known at compile time, thereby allowing the compiler to anticipate all potential return types of request
. Unfortunately, I am uncertain about how to implement this!