0

We have a gRPC repo that has a JS implementation of creating a client.

/**
 * Code generated by protoc-gen-twirp_js v5.0.0, DO NOT EDIT.
 * source: domain_service/service.proto
 */
// import our twirp js library dependency
var createClient = require("twirp");
// import our protobuf definitions
var pb = require("./domain_service/service_pb.js");
Object.assign(module.exports, pb);

/**
 * Creates a new DomainClient
 */
module.exports.createDomainClient = function(baseurl, extraHeaders, useJSON) {
    var rpc = createClient(baseurl, "events.domain_service.Domain", "v5.0.0",  useJSON, extraHeaders === undefined ? {} : extraHeaders);
    return {
        fireDomainEvent: function(data) { return rpc("FireDomainEvent", data, pb.FireDomainEventResponse); }
    }
}

I created a d.ts file in the same folder using TS playground

export function createDomainClient(
  baseurl: any,
  extraHeaders: any,
  useJSON: any
): {
  fireDomainEvent: (data: any) => any;
};

But when I try to implement the code:

import { createDomainClient } from '@MyOrganization/package';

const client = new createDomainClient(
  'https://my.url.com', '', false
);

I get the follow error: 'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.ts(7009)

I am simply trying to import the commonJS client into my Typescript project with making the least amount of changes on the gRPC repo side.

Orbita1frame
  • 203
  • 4
  • 13
  • Just as a tip, you can write your declarations into the file until TS is happy, then you can move them to a d.ts declaration file afterwards. Much easier to work out of a single file, and you can post a single snippet if you have a question. – JΛYDΞV Oct 13 '22 at 09:12

1 Answers1

1

Either explicitly set any type for client

import { createDomainClient } from '@MyOrganization/package';

const client: any = new createDomainClient(
  'https://my.url.com', '', false
);

Or add a construct signature to the type How does interfaces with construct signatures work?

Konrad
  • 21,590
  • 4
  • 28
  • 64
  • Hmmm. That doesn't seem to work. I am getting an error on `new createDomainClient( 'https://my.url.com', '', false );` part of the statement. – Orbita1frame Oct 11 '22 at 21:17
  • 1
    Figured out that removing the `new` keyword solved the issue. The function wasn't specified as a class in the d.ts file. – Orbita1frame Oct 11 '22 at 23:04