Type 'GrpcObject' has no construct signatures _ typescript grpc

848 views Asked by At

I am new in ts . I am working with nest. I am trying to use my grpc-client I wrote with js . but when I try to export my grpc service object I get this error:

import {loadPackageDefinition,credentials} from '@grpc/grpc-js';
import {loadSync} from '@grpc/proto-loader';

const packageDefinition = loadSync(`${__dirname}/protos/main.proto`, {
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true,
    arrays: true
});


let { AuthService } = loadPackageDefinition(packageDefinition) ;

 export const auth_client = new AuthService(`localhost:${process.env.auth_grpc_port?process.env.auth_grpc_port:30030}`,credentials.createInsecure());

error :

src/lib/grpc.client.ts:16:34 - error TS2351: This expression is not constructable. Not all constituents of type 'GrpcObject | ServiceClientConstructor | ProtobufTypeDefinition' are constructable. Type 'GrpcObject' has no construct signatures.

1

There are 1 answers

0
murgatroid99 On

Unfortunately, the TypeScript types for the object output by loadPackageDefinition are too generic to be used that way effectively. One solution would be to use the TypeScript generator distributed with @grpc/proto-loader version 0.6.x to generate TypeScript types for your proto files. Then you can use a type assertion to make the output of loadPackageDefinition have a specific structure that reflects your proto files.