Skip to content

Instantly share code, notes, and snippets.

@mcheshkov
Created December 14, 2023 00:33
Show Gist options
  • Save mcheshkov/d0f8b5fa733f87082265320c371b751b to your computer and use it in GitHub Desktop.
Save mcheshkov/d0f8b5fa733f87082265320c371b751b to your computer and use it in GitHub Desktop.
ts-proto type declaration problem sample
import * as _m0 from "protobufjs/minimal";
export declare const protobufPackage = "TsProtoSample";
export interface FooRequest {
req: string;
}
export interface FooResponse {
res: string;
}
export declare const FooRequest: {
encode(message: FooRequest, writer?: _m0.Writer): _m0.Writer;
decode(input: _m0.Reader | Uint8Array, length?: number): FooRequest;
fromJSON(object: any): FooRequest;
toJSON(message: FooRequest): unknown;
create<I extends {
req?: string;
} & {
req?: string;
} & { [K in Exclude<keyof I, "req">]: never; }>(base?: I): FooRequest;
fromPartial<I_1 extends {
req?: string;
} & {
req?: string;
} & { [K_1 in Exclude<keyof I_1, "req">]: never; }>(object: I_1): FooRequest;
};
export declare const FooResponse: {
encode(message: FooResponse, writer?: _m0.Writer): _m0.Writer;
decode(input: _m0.Reader | Uint8Array, length?: number): FooResponse;
fromJSON(object: any): FooResponse;
toJSON(message: FooResponse): unknown;
create<I extends {
res?: string;
} & {
res?: string;
} & { [K in Exclude<keyof I, "res">]: never; }>(base?: I): FooResponse;
fromPartial<I_1 extends {
res?: string;
} & {
res?: string;
} & { [K_1 in Exclude<keyof I_1, "res">]: never; }>(object: I_1): FooResponse;
};
export type FooDefinition = typeof FooDefinition;
export declare const FooDefinition: {
readonly name: "Foo";
readonly fullName: "TsProtoSample.Foo";
readonly methods: {
readonly foo: {
readonly name: "foo";
readonly requestType: {
encode(message: FooRequest, writer?: _m0.Writer): _m0.Writer;
decode(input: _m0.Reader | Uint8Array, length?: number): FooRequest;
fromJSON(object: any): FooRequest;
toJSON(message: FooRequest): unknown;
create<I extends {
req?: string;
} & {
req?: string;
} & { [K in Exclude<keyof I, "req">]: never; }>(base?: I): FooRequest;
fromPartial<I_1 extends {
req?: string;
} & {
req?: string;
} & { [K_1 in Exclude<keyof I_1, "req">]: never; }>(object: I_1): FooRequest;
};
readonly requestStream: false;
readonly responseType: {
encode(message: FooResponse, writer?: _m0.Writer): _m0.Writer;
decode(input: _m0.Reader | Uint8Array, length?: number): FooResponse;
fromJSON(object: any): FooResponse;
toJSON(message: FooResponse): unknown;
create<I_2 extends {
res?: string;
} & {
res?: string;
} & { [K in Exclude<keyof I, "res">]: never; }>(base?: I_2): FooResponse;
fromPartial<I_3 extends {
res?: string;
} & {
res?: string;
} & { [K_1 in Exclude<keyof I_1, "res">]: never; }>(object: I_3): FooResponse;
};
readonly responseStream: false;
readonly options: {};
};
};
};
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
export type DeepPartial<T> = T extends Builtin ? T : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {} ? {
[K in keyof T]?: DeepPartial<T[K]>;
} : Partial<T>;
type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin ? P : P & {
[K in keyof P]: Exact<P[K], I[K]>;
} & {
[K in Exclude<keyof I, KeysOfUnion<P>>]: never;
};
export {};
syntax = "proto3";
package TsProtoSample;
message FooRequest {
string req = 1;
}
message FooResponse {
string res = 1;
}
service Foo {
rpc foo(FooRequest) returns (FooResponse) {}
}
/* eslint-disable */
import * as _m0 from "protobufjs/minimal";
export const protobufPackage = "TsProtoSample";
export interface FooRequest {
req: string;
}
export interface FooResponse {
res: string;
}
function createBaseFooRequest(): FooRequest {
return { req: "" };
}
export const FooRequest = {
encode(message: FooRequest, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.req !== "") {
writer.uint32(10).string(message.req);
}
return writer;
},
decode(input: _m0.Reader | Uint8Array, length?: number): FooRequest {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseFooRequest();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
if (tag !== 10) {
break;
}
message.req = reader.string();
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},
fromJSON(object: any): FooRequest {
return { req: isSet(object.req) ? globalThis.String(object.req) : "" };
},
toJSON(message: FooRequest): unknown {
const obj: any = {};
if (message.req !== "") {
obj.req = message.req;
}
return obj;
},
create<I extends Exact<DeepPartial<FooRequest>, I>>(base?: I): FooRequest {
return FooRequest.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<FooRequest>, I>>(object: I): FooRequest {
const message = createBaseFooRequest();
message.req = object.req ?? "";
return message;
},
};
function createBaseFooResponse(): FooResponse {
return { res: "" };
}
export const FooResponse = {
encode(message: FooResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.res !== "") {
writer.uint32(10).string(message.res);
}
return writer;
},
decode(input: _m0.Reader | Uint8Array, length?: number): FooResponse {
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseFooResponse();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
if (tag !== 10) {
break;
}
message.res = reader.string();
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message;
},
fromJSON(object: any): FooResponse {
return { res: isSet(object.res) ? globalThis.String(object.res) : "" };
},
toJSON(message: FooResponse): unknown {
const obj: any = {};
if (message.res !== "") {
obj.res = message.res;
}
return obj;
},
create<I extends Exact<DeepPartial<FooResponse>, I>>(base?: I): FooResponse {
return FooResponse.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<FooResponse>, I>>(object: I): FooResponse {
const message = createBaseFooResponse();
message.res = object.res ?? "";
return message;
},
};
export type FooDefinition = typeof FooDefinition;
export const FooDefinition = {
name: "Foo",
fullName: "TsProtoSample.Foo",
methods: {
foo: {
name: "foo",
requestType: FooRequest,
requestStream: false,
responseType: FooResponse,
responseStream: false,
options: {},
},
},
} as const;
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
export type DeepPartial<T> = T extends Builtin ? T
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;
type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };
function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
{
"name": "ts-proto-sample",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build:protoc": "grpc_tools_node_protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=./ --ts_proto_opt=outputServices=generic-definitions ./a.proto",
"build:tsc": "tsc --declaration ./a.ts",
"build": "npm run build:protoc && npm run build:tsc"
},
"author": "",
"license": "ISC",
"dependencies": {
"grpc-tools": "1.12.4",
"ts-proto": "1.165.1",
"typescript": "5.3.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment