I have following types:
export interface TypedDocumentNode<R, V> {}
type ClaimResult = {
id: string;
status: string;
};
type ClaimInput = {
code: string;
};
type ResetResult = {
id: string;
userId: string;
};
type ResetInput = {
email: string;
};
type Operations = {
Claim: TypedDocumentNode<ClaimResult, ClaimInput>;
Reset: TypedDocumentNode<ResetResult, ResetInput>
};
I am trying to write generic function execute
such that it shall take operation name (keyof of operations type) and respective input as variables and then returns the corresponding result. For example:
// Valid possibilities
execute('Reset', { email: '[email protected]' });
execute('Claim', { code: 'test-unique-code' });
This is my type definition for execute
function:
export async function execute<
Operation extends keyof typeof Operations,
Document extends Documents[Operation],
TResult extends Document extends TypedDocumentNode<infer X, any> ? X : never,
TVariables extends Document extends TypedDocumentNode<TResult, infer V> ? V : never,
> (
operation: Operation,
variables?: TVariables
): Promise<TResult> {
// Rest of the definition.
}
However, the problem is that this definition allows other input types as well:
// Should be invalid combination
execute('Claim', { email: '[email protected]' });
Is this possible to achieve this in TypeScript?
4