Skip to content

Generated Service Definition types are inaccurate #1740

@mintchkin

Description

@mintchkin

Problem description

The typescript generator introduced in #1474 produces an incorrect/incomplete type for services in loaded protos. Referencing the golden-generated output, the type of the service field is ServiceDefinition.

This causes problems with the type of server.addService in the native grpc package:

const packageDefinition = (protoLoader.loadSync(...) as unknown) as ProtoGrpcType;
const server = new grpc.Server();
server.addService(
    packageDefinition.google.showcase.v1beta1.Echo.service,
    // ~~~~~~ TS: Argument of type 'ServiceDefinition' is not assignable to parameter of type 'ServiceDefinition<{ Block: ..., Chat: ..., ...}>'
    { Block: ..., Chat: ..., ... }
)

I think the type can be improved without changing the generated code much by injecting a custom ServiceDefinition type based on the one in the native grpc package, or maybe the related one in @grpc/proto-loader could just be replaced instead.

type ServiceDefinition<
  ImplementationType extends grpc.UntypedServiceImplementation = grpc.UntypedServiceImplementation
> = {
  [K in keyof ImplementationType]: MethodDefinition<
    Parameters<ImplementationType[K]>[0], // Request Type
    ReturnType<ImplementationType[K]> // Return Type
  >;
};

// ... later in e.g. echo.ts

Echo: SubtypeConstructor<typeof grpc.Client, _google_showcase_v1beta1_EchoClient> & { service: ServiceDefinition<EchoHandlers> }

Environment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions