twilio / twilio-node

Node.js helper library
MIT License
1.37k stars 497 forks source link

How to get Twilio class type? #913

Closed jgeurts closed 1 year ago

jgeurts commented 1 year ago

Issue Summary

With v3.84, we were able to pull in the type of the Twilio client and reference it in our application:

import { Twilio } from 'twilio';

export async function doSomething(twilioClient: Twilio) {
  const message = await twilioClient.messages.create({ ... });
  console.log(`Success: ${message.sid}`);
}

However, now with v4.7, the same code not longer works, with the following message:

error TS2749: 'Twilio' refers to a value, but is being used as a type here. Did you mean 'typeof Twilio'

However, typeof Twilio does not work and is not what we're looking for. We are looking to reference an instance of the Twilio class. Any advice on how to do this with v4?

alexander-reyes-wainwright commented 1 year ago

We've also just found this on a new project when attempting to use twilio@4.7.1 - seems like the twilio client type definition isn't exported, or is exported incorrectly, compared to twilio@3.84.1.

Seems like it either:

needs to be fixed at the typing itself or needs a new example on how to define typescript types properly for the new way of instantiating the twilio client if that has drastically changed.

aaronhuggins-carewell commented 1 year ago

We also found this to be an issue on upgrading one of our projects. I've submitted #914 which should fix this.

JenniferMah commented 1 year ago

Hi @jgeurts! Thank you for bringing this to our attention and thank you @aaronhuggins-carewell for opening a PR to solve this bug! Our team will review #914 and we will update this issue with the expected release date for the next version of the twilio-node library that will include a fix.

aaronhuggins commented 1 year ago

@JenniferMah Thanks! I'll run Prettier in the AM and push. Should otherwise be ready for review.

aaronhuggins-carewell commented 1 year ago

@JenniferMah Corrected style formatting. Everything appears to pass locally running npm run ci. I had only run jscs and test before.

Hunga1 commented 1 year ago

Thanks again, @aaronhuggins-carewell for opening a PR (#914) to solve this issue. I've gone ahead and merged your changes. These changes will be included in our next version release of the library, expected on February 22nd.

sattarab commented 1 year ago

when doing

import { Twilio } from "twilio/lib"
const twilio_client = new Twilio( <SID>, <AUTH> )

the type of twilio_client is any in version 4 where as in version 3 it used to be of the correct type. Please do let me know if I am doing something wrong here