Skip to content

Commit

Permalink
Improve types
Browse files Browse the repository at this point in the history
Signed-off-by: Sri Krishna Paritala <[email protected]>
  • Loading branch information
srikrsna-buf committed Oct 8, 2024
1 parent ee3e7b6 commit 52587cc
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions packages/protobuf/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,47 +111,31 @@ export type DescMethodStreaming<
export type DescMethodUnary<
I extends DescMessage = DescMessage,
O extends DescMessage = DescMessage,
> = DescMethodCommon & {
methodKind: "unary";
input: I;
output: O;
};
> = DescMethodTyped<"unary", I, O>;

/**
* Describes a server streaming RPC declaration.
*/
export type DescMethodServerStreaming<
I extends DescMessage = DescMessage,
O extends DescMessage = DescMessage,
> = DescMethodCommon & {
methodKind: "server_streaming";
input: I;
output: O;
};
> = DescMethodTyped<"server_streaming", I, O>;

/**
* Describes a client streaming RPC declaration.
*/
export type DescMethodClientStreaming<
I extends DescMessage = DescMessage,
O extends DescMessage = DescMessage,
> = DescMethodCommon & {
methodKind: "client_streaming";
input: I;
output: O;
};
> = DescMethodTyped<"client_streaming", I, O>;

/**
* Describes a bidi streaming RPC declaration.
*/
export type DescMethodBiDiStreaming<
I extends DescMessage = DescMessage,
O extends DescMessage = DescMessage,
> = DescMethodCommon & {
methodKind: "bidi_streaming";
input: I;
output: O;
};
> = DescMethodTyped<"bidi_streaming", I, O>;

/**
* The init type for a message, which makes all fields optional.
Expand Down Expand Up @@ -184,4 +168,17 @@ type OneofSelectedMessage<K extends string, M extends Message> = {
value: M;
};

type DescMethodCommon = Omit<DescMethod, "methodKind" | "input" | "output">;
type DescMethodTyped<K extends DescMethod["methodKind"], I extends DescMessage, O extends DescMessage> = Omit<DescMethod, "methodKind" | "input" | "output"> & {
/**
* One of the four available method types.
*/
readonly methodKind: K;
/**
* The message type for requests.
*/
readonly input: I;
/**
* The message type for responses.
*/
readonly output: O;
}

0 comments on commit 52587cc

Please sign in to comment.