Orbita1frame
Orbita1frame

Reputation: 301

How do I import this commonJS module into my Typescript with the least amount of changes

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.

Upvotes: 0

Views: 318

Answers (1)

Konrad
Konrad

Reputation: 24681

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?

Upvotes: 1

Related Questions