question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't instantiate client in typescript: Only a void function can be called with the 'new' keyword

See original GitHub issue

Version: 3.19.2

Code Snippet

import * as TwilioApi from 'twilio';
const twilio = new TwilioApi('sid', 'secret');

Exception/Log

Typescript error: Only a void function can be called with the 'new' keyword 

If you don’t use new keyword, the constructor doesn’t get called and there’s a different error. I’m just going to use the REST Api directly, but this was certainly annoying/disappointing. I’d normally just ignore it, but I’m using Google Cloud functions, and don’t want to turn off their TS based safety feature, which prevents code that throws errors from deploying.

Steps to Reproduce

  1. Use typescript with twilio-node
  2. Try to instantiate a client
  3. Breaks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
philnashcommented, Feb 28, 2021

I have been using the Twilio library (see this post on sending SMS messages with Twilio in TypeScript) like this:

import { Twilio } from "twilio";
const client = new Twilio(accountSid, authToken);

This picks out the actual Twilio API client.

3reactions
j0hnm4r5commented, Feb 28, 2021

I too am getting this issue as of Feb 2021. This removes the error for me:

import Twilio from "twilio";

const client = new (Twilio as any)(
    process.env.TWILIO_ACCOUNT_SID,
    process.env.TWILIO_AUTH_TOKEN
);
Read more comments on GitHub >

github_iconTop Results From Across the Web

TS - Only a void function can be called with the 'new' keyword
I am getting this weird error from TypeScript:.
Read more >
new operator - JavaScript - MDN Web Docs - Mozilla
When a function is called with the new keyword, the function will be used as a constructor. new will do the following things:....
Read more >
Handbook - Classes - TypeScript
Here we create a new variable called greeterMaker . This variable will hold the class itself, or said another way its constructor function....
Read more >
TypeScript Interfaces - TutorialsTeacher
This is also known as "duck typing" or "structural subtyping". An interface is defined with the keyword interface and it can include properties...
Read more >
Docs • Svelte
npm create svelte@latest myapp cd myapp npm install npm run dev. SvelteKit will handle calling the Svelte compiler to convert your .svelte files...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found