javascripttypescripttwilioprism

Typescript: this expression is not constructable when trying to import a class


I'm specifically trying to follow Twilios Mock API guide here: https://www.twilio.com/docs/openapi/mock-api-generation-with-twilio-openapi-spec

I've followed their guide, except for changing the JS file into TS, because as a JS file I have the same error anyway.

The code:

import * as twilio from 'twilio';
import { verifyServiceId, accountSid, authToken } from '../constants';
import * as PrismClient from './PrismClient';


const client = new twilio.Twilio(accountSid, authToken);

const client = new twilio.Twilio(accountSid, authToken, {
  httpClient: new PrismClient('http://0.0.0.0:4010', new twilio.RequestClient()),
});

The error is this:

This expression is not constructable. Type 'typeof import("/sms/lib/services/PrismClient")' has no construct signatures.

In the twilio guide they are using JS files, assuming I am importing into a JS file where my Node client is.

How can I use this mock client within my TS files? Help would be hugely appreciated, banging my head against this!

The prism class is the same as in the guide:

export class PrismClient {
constructor(prismUrl, requestClient) {
  this.prismUrl = prismUrl;
  this.requestClient = requestClient;
}

request(opts) {
  opts.uri = opts.uri.replace(/^https\:\/\/.*?\.twilio\.com/, this.prismUrl);
  return this.requestClient.request(opts);
}
}

Solution

  • That's because you're doing a namespaced import but using the namespace itself instead of the item you want:

    import { PrismClient } from './PrismClient';