Search code examples
node.jstypescripttwilio

How can I use Twilio node with TypeScript?


My code is:

import * as twilio from 'twilio';
import { IncomingCall } from '../models/IncomingCall';
const VoiceResponse = twilio.twiml.VoiceResponse;
export function incoming(requestBody: any): any {
  const twiml = new VoiceResponse();

  return IncomingCall.create({
    CallSid: requestBody.CallSid,
    From: requestBody.From,
    To: requestBody.To,
    rawData: requestBody
  });
}

But I get an error when doing new VoiceResponse():

[ts] Cannot use 'new' with an expression whose type lacks a call or construct signature.

I'm using Twilio v3.19.2


Solution

  • You need to install the @types/twilio package which contains the type definitions for twilio.

    Run npm install @types/twilio (or yarn add @types/twilio if you use yarn).

    You can read more about the @types/ packages here.