Skip to content

Add constructor based initialization for CJS based TypeScript imports#2669

Merged
prathmesh-stripe merged 3 commits intomasterfrom
prathmesh/stripe-constructor
Apr 8, 2026
Merged

Add constructor based initialization for CJS based TypeScript imports#2669
prathmesh-stripe merged 3 commits intomasterfrom
prathmesh/stripe-constructor

Conversation

@prathmesh-stripe
Copy link
Copy Markdown
Contributor

@prathmesh-stripe prathmesh-stripe commented Apr 7, 2026

Why?

CJS TypeScript users who write import Stripe = require('stripe') get no type information from StripeConstructor it's untyped, so new Stripe() and Stripe() both resolve to any. This makes the CJS TypeScript experience significantly worse than ESM, where full types are available.

What?

  • Added a StripeCallableConstructor type to stripe.cjs.node.ts and stripe.cjs.worker.ts that intersects typeof Stripe (construct signature + static members) with a call signature.
  • Now both new Stripe(key, config) and Stripe(key, config) are fully typed for CJS TypeScript consumers
  • Added a new testProjects/cjs-ts/ integration test project that validates CJS TypeScript usage: constructor patterns, static member access, instance members, error handling, and instanceof checks
  • Expanded the existing testProjects/cjs/index.js test to cover both callable and new patterns, static property access, and instanceof checks

See Also

#2660

Changelog

  • Initialization of Stripe class with new keyword is now possible for CJS based TypeScript project. Resolves: 2660
import Stripe = require('stripe');
// ✅ Both statements work
const stripeNew: Stripe.Stripe = new Stripe('sk_test_...');
const stripeCalled: Stripe.Stripe = Stripe('sk_test_...');

@prathmesh-stripe prathmesh-stripe marked this pull request as ready for review April 7, 2026 23:48
@prathmesh-stripe prathmesh-stripe requested a review from a team as a code owner April 7, 2026 23:48
@prathmesh-stripe prathmesh-stripe requested review from mbroshi-stripe and xavdid and removed request for a team April 7, 2026 23:48
@prathmesh-stripe prathmesh-stripe changed the title Prathmesh/stripe constructor Add constructor based initialization for CJS based TypeScript imports Apr 7, 2026
Comment thread testProjects/cjs-ts/index.ts
@prathmesh-stripe prathmesh-stripe enabled auto-merge (squash) April 8, 2026 02:37
@prathmesh-stripe prathmesh-stripe merged commit 15ad8fb into master Apr 8, 2026
9 checks passed
@prathmesh-stripe prathmesh-stripe deleted the prathmesh/stripe-constructor branch April 8, 2026 02:40
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.

TypeScript error when initializing Stripe client after upgrading to v22

2 participants