Skip to content

Fix TypeScript named import support for Twilio client #1109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Aug 1, 2025

Fixes #935 where importing { Twilio } from the library resulted in TypeScript inferring the type as any instead of the proper Twilio class type.

Problem

Users trying to use ES6 named imports would encounter type issues:

import { Twilio } from "twilio";
const client = new Twilio("ACxxx", "token"); // Type was inferred as 'any'

The root cause was that the library used export = TwilioSDK syntax throughout, which requires esModuleInterop: true in TypeScript configuration to work properly with named imports. Without this setting, TypeScript couldn't resolve the named exports correctly.

Solution

Modified src/index.ts to provide both ES6 named exports and CommonJS compatibility:

  • Before: export = TwilioSDK
  • After: Added explicit named exports (export const Twilio, export const jwt, etc.) + export default TwilioSDK

This generates a lib/index.d.ts file that supports all import patterns:

// ✅ ES6 named import (previously broken)
import { Twilio } from "twilio";

// ✅ Default import (continues to work)  
import twilio from "twilio";

// ✅ Function call (continues to work)
const client = twilio("ACxxx", "token");

// ✅ Constructor access (continues to work)
const client = new twilio.Twilio("ACxxx", "token");

Testing

  • All existing tests pass (316 tests)
  • TypeScript compilation succeeds with proper type inference
  • Verified backward compatibility with all existing import patterns
  • No breaking changes to existing APIs

The fix ensures users can use modern ES6 import syntax without requiring specific TypeScript configuration, while maintaining full backward compatibility.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

- Replace `export = TwilioSDK` with ES6-compatible export pattern in src/index.ts
- Add explicit named exports (export const Twilio, jwt, twiml, etc.)
- Add default export for backward compatibility (export default TwilioSDK)
- Generated lib/index.d.ts now provides both CommonJS and ES6 import support
- Fixes GitHub issue #935 where import { Twilio } returned 'any' type

The change allows users to use both import patterns:
- import { Twilio } from 'twilio' (ES6 named import - previously broken)
- import twilio from 'twilio' (default import - continues to work)
- twilio() function calls and new twilio.Twilio() - continues to work

Resolves: #935

Co-authored-by: manisha1997 <[email protected]>
@Copilot Copilot AI changed the title [WIP] @twilio/twilio-node/issues/935 I want a PR to be raised for this Fix TypeScript named import support for Twilio client Aug 1, 2025
Copilot finished work on behalf of manisha1997 August 1, 2025 07:35
@Copilot Copilot AI requested a review from manisha1997 August 1, 2025 07:35
Copy link

sonarqubecloud bot commented Aug 1, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Twilio client is of type of any when using Twilio import in typescript
2 participants