The type 'GrpcObject' in TypeScript grpc does not contain any construct signatures

Just starting out in TypeScript and diving into Nest. I'm attempting to use my JS-written grpc-client, but running into an issue when exporting the grpc service object. Here's the code snippet:


        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 message received:

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.

Answer №1

It is regretful that the TypeScript types associated with the object produced by the loadPackageDefinition function are excessively broad to be effectively utilized in this manner. One potential resolution could involve employing the TypeScript generator that comes with version 0.6.x of @grpc/proto-loader to create specific TypeScript types for your proto files. Subsequently, you can implement a type assertion to structure the output of loadPackageDefinition in a way that accurately reflects the contents of your proto files.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for configuring VS Code to display and check object schemas

npm init -y npm i axios npm i @types/axios --save-dev Why doesn't VS Code 1.62 seem to provide the response object schema when typing code like this: resp = await axios("https://httpstat.us/404"); resp. <C-Space> displays confusing / inappropr ...

Are you looking to utilize both `Boolean()` and `!!` at the same time,

Boolean type automatically infers to be a boolean type, while the !! operator is used to force a value to either true or false. Is this behavior intentional and if so, why? Below is the code snippet for reference: const truthy = true const falsy = false c ...

What steps can I take to stop typescript tsc from compiling the ../node_modules directory?

I'm currently working on a project and I decided to clone the typescript/node/express sample project into a subfolder. However, when I try running tsc -p . in the sub-folder, I encounter compilation errors because tsc is scanning ../node_modules. Is t ...

import and export a TypeScript type alias from a .d.ts module

Is there a way to create a file named vue-number.d.ts containing the following declaration: export type vueNumber = number | ''; and then use this type alias in multiple TypeScript modules? If I attempt to do so, the import statement gives an ...

In strict mode, it is not allowed for an object literal to contain duplicate property names

Here is the code snippet I am having trouble with: import { combineReducers } from 'redux'; import { postReducers } from './postReducers'; import { stationsReducer } from './TrackCircuitSensorDataFormReducers/StationsReducer' ...

When compiling TypeScript, the exported module cannot be located

I've encountered an issue while working on my TypeScript project. Upon compiling my code with the following configuration: { "compilerOptions": { "target": "ESNext", "module": "ESNext", & ...

Guide on validating multiple preselected checkboxes using Angular 8 reactive forms

I have a problem with validating checkboxes in my Angular application. The checkboxes are generated dynamically from an array using ngFor loop, and I want to make sure that at least one checkbox is selected before the form can be submitted. The validatio ...

What is the proper way to code this complex function using Typescript?

This is an interesting function: function extractKey(obj, key) { var result = {}; Object.keys(obj).forEach(k => { result[k] = () => k; }); return key(result)(); } Here is a variation of the code: getKey<T>(obj: T, keyGetter: (o ...

Tips for efficiently constructing a Docker container using nodejs and TypeScript

Struggling for the past couple of days to get the project running in production, but it keeps throwing different errors. The most recent one (hopefully the last!) is: > rimraf dist && tsc -p tsconfig.build.json tsc-watch/test/fixtures/failing.t ...

The type 'number | { percent: number; }' cannot be assigned to the type 'string | number | undefined' according to ts(2322) error message

Presently, I am engaged in a project utilizing React and Typescript, where I am working on implementing a progress bar using react semantic-ui. I have encountered a typescript error in my code. Here is the segment causing the issue: import React, { Compo ...

What is the best way to categorize a collection of objects within a string based on their distinct properties?

I am working with an array of hundreds of objects in JavaScript, each object follows this structure : object1 = { objectClass : Car, parentClass : Vehicle, name : BMW } object2 = { objectClass : Bicycle, parentClass : Vehicle, name : Giant } object3 = { ob ...

Finding the class type that implements an interface: A guide

Consider this scenario: interface IAnInterface { } How can you refer to and point to a class type that implements the interface? For instance, if we have a class like this: class AClassThatImplmentsAnInterface implements IAnInterface { } What is the m ...

Replace string values with an object array for a particular property

When faced with the task of replacing specific string values defined by the user in an array of objects, but only applying the rules to certain properties, there is a need for a more efficient approach. For instance, consider the array below: [ {Name: &apo ...

"Continue to shine until the rendering function displays its source code

I've encountered a strange issue where I'm using lit until to wait for a promise to return the template, but instead of the desired output, the until's function code is being rendered. For example: render() { return html` <div c ...

Eslint is unable to locate file paths when it comes to Solid.js tsx extensions

Whenever I attempt to import TSX components (without the extension), Eslint raises an error stating that it's unable to resolve the path: https://i.sstatic.net/NiJyU.png However, if I add the TSX extension, it then prompts me to remove it: https:// ...

Angular: Ensuring Elements inside mat-stepper within mat-dialog Adjust to the Height of the Dialog

I am dealing with a mat-dialog that contains a mat-stepper. Within this stepper, there is a step that includes a form with various elements such as a list, mat-card, and table. I need these elements to always fill the height of the dialog. Specifically, I ...

Discover similar items within an array by utilizing async/await and promise.all

The value of filterdList.length always equals the total number of elements with the code provided below. As a result, this method consistently returns false because there is only one item in the table that matches the given name. async itemExists(name) : ...

Maintaining the order of subscribers during asynchronous operations can be achieved by implementing proper synchronization

In my Angular setup, there is a component that tracks changes in its route parameters. Whenever the params change, it extracts the ID and triggers a function to fetch the corresponding record using a promise. Once the promise resolves, the component update ...

The TypeScript factory class anticipates an intersection

In my code, I have a class factory called pickSomething that generates a specific type based on a key provided from a ClassMap: class A { keya = "a" as const; } class B { keyb = "b" as const; } type ClassMap = { a: A b: B } c ...

tying the [(ngModel)] to a value that is not defined

Currently facing an issue with the [(ngModel)] below as I am not receiving any values from the backend: <input type="number" [(ngModel)]="list.test[0]"> The error arises due to the absence of values in the test array from the backend, resulti ...