TypeScript Type Inference Issue with createServerFn and Connect RPC
I'm experiencing a TypeScript type inference issue when using
createServerFn().handler() with a Connect RPC function that returns a discriminated union response type.
TypeScript recursively infers the entire getCurrentUser response structure, deeply expanding all nested Protobuf message types within the discriminated union.
This results in a massive inferred type that:
- Is not assignable to UserAccountDetails, causing a type error
- Causes Cursor IDE to completely hang/freeze, making the file unusable
Code Example:
// My Connect RPC response type (discriminated union)
type ConnectRpcResponse<TData extends Message> =
| { readonly data: TData; readonly error?: undefined; }
| { readonly data?: undefined; readonly error: ConnectError; };
// My getCurrentUser function returns this type
export async function getCurrentUser(
payload: ConnectRpcRequestPayload<GetCurrentUserRequest>
): Promise<ConnectRpcResponse<UserAccountDetails>> {
// ... implementation
return { data: response.account.value }; // UserAccountDetails
}
// Trying to use it with createServerFn
const beforeLoad = createServerFn({ method: 'GET' }).handler(async function() {
const data = await getCurrentUser({});
return data; // ❌ TypeScript error here causes Cursor IDE to completely hang/freeze,
});
0 Replies