Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 1, 2025

Issue

Fixes #935 where import { Twilio } from "twilio/lib" resulted in the Twilio constructor being typed as any instead of the proper Twilio type, causing TypeScript to lose type safety and intellisense support.

Root Cause

The issue occurred because the generated TypeScript definitions in lib/index.d.ts used the export = TwilioSDK syntax with namespace declarations that contained both type and value declarations with the same names. When TypeScript tried to resolve named imports like { Twilio } from modules using export =, it couldn't properly infer the correct type and fell back to any.

Before (Broken)

import { Twilio } from "twilio/lib";

const client = new Twilio(accountSid, authToken);
// ❌ 'client' was typed as 'any' - no type safety or intellisense

// This would compile without errors (bad!)
const invalidAssignment: string = client;

After (Fixed)

import { Twilio } from "twilio/lib";

const client = new Twilio(accountSid, authToken);
// ✅ 'client' is now properly typed as 'Twilio' instance

// This now correctly produces a TypeScript error
const invalidAssignment: string = client; // Error: Type 'Twilio' is not assignable to type 'string'

Solution

Enhanced the generated TypeScript definitions in lib/index.d.ts to provide proper value declarations in the namespace alongside the existing type declarations. This enables TypeScript to correctly resolve named imports while maintaining full backward compatibility with existing CommonJS usage patterns.

The fix:

  1. Preserves the original export = TwilioSDK for CommonJS compatibility
  2. Adds proper namespace value declarations for all exported members
  3. Enables TypeScript to resolve import { Twilio } to the correct constructor type
  4. Maintains all existing functionality and API compatibility

Testing

  • ✅ All existing tests pass
  • ✅ TypeScript compilation succeeds
  • ✅ Both CommonJS and ES module import patterns work correctly
  • ✅ Proper type inference is restored for named imports

This change resolves the frustrating developer experience issue where users lost type safety when using the more modern destructuring import syntax, while ensuring no breaking changes for existing code.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@manisha1997 manisha1997 closed this Aug 1, 2025
@Copilot Copilot AI changed the title [WIP] @twilio/twilio-node/issues/935 Fix TypeScript named import typing issue - resolves 'any' type for destructured imports Aug 1, 2025
Copilot finished work on behalf of manisha1997 August 1, 2025 07:47
@Copilot Copilot AI requested a review from manisha1997 August 1, 2025 07:47
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