I have the following types that I'm planning on use as the Return Type from a function
export interface ServerActionSuccess<TData> {
error: null;
data: TData;
}
export interface ServerActionError<TError> {
error: TError;
data: null;
}
export type ServerActionResult<TData, TError> =
| ServerActionSuccess<TData>
| ServerActionError<TError>;
Here's an example of trying to use it:
export const getTransactionsAction = async (
data: z.infer<typeof GetTransactionsActionInput>,
): Promise<ServerActionResult> => {
const validation = GetTransactionsActionInput.safeParse(data);
if (!validation.success) return { error: validation.error, data: null };
return {
error: null,
data: await getTransactions({ page: data.page, userId: "" }),
};
};
The problem is that ServerActionResult takes 2 generic arguments. I'm wondering if there's any way I can make typescript infer those types for me.
Right now if I try to call this function both types data and error are infered as any:
const { data, error } = await getTransactionsAction({ page: 1 });