Skip to content

Commit

Permalink
Add generic result parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrtenz committed Oct 6, 2022
1 parent 277f582 commit d0479d0
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,13 @@ export const PendingJsonRpcResponseStruct = object({

/**
* A JSON-RPC response object that has not yet been resolved.
*
* Note that TypeScript infers `unknown | undefined` as `unknown`, meaning that
* the `result` and `error` fields are not optional. To make them optional, we
* use the `OptionalField` helper, to explicitly make them optional.
*/
export type PendingJsonRpcResponse = OptionalField<
export type PendingJsonRpcResponse<Result extends Json> = Omit<
Infer<typeof PendingJsonRpcResponseStruct>,
'result'
>;
> & {
result?: Result;
};

export const JsonRpcSuccessStruct = object({
id: JsonRpcIdStruct,
Expand Down Expand Up @@ -291,7 +289,7 @@ export type JsonRpcResponse<Result extends Json> =
*/
export function isPendingJsonRpcResponse(
response: unknown,
): response is PendingJsonRpcResponse {
): response is PendingJsonRpcResponse<Json> {
return is(response, PendingJsonRpcResponseStruct);
}

Expand All @@ -304,7 +302,7 @@ export function isPendingJsonRpcResponse(
*/
export function assertIsPendingJsonRpcResponse(
response: unknown,
): asserts response is PendingJsonRpcResponse {
): asserts response is PendingJsonRpcResponse<Json> {
try {
assert(response, PendingJsonRpcResponseStruct);
} catch (error) {
Expand Down

0 comments on commit d0479d0

Please sign in to comment.