Skip to content

Add type safety to Stripe constructor config (no runtime change)#2667

Merged
mbroshi-stripe merged 4 commits intomasterfrom
mbroshi/type-safe-config
Apr 8, 2026
Merged

Add type safety to Stripe constructor config (no runtime change)#2667
mbroshi-stripe merged 4 commits intomasterfrom
mbroshi/type-safe-config

Conversation

@mbroshi-stripe
Copy link
Copy Markdown
Contributor

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

Why?

The Stripe constructor's second parameter was typed as Record<string, unknown>, which meant TypeScript users got no autocomplete, no excess property checking, and no documentation on hover. This was reported in #2665. This was caused by a type regression in our recent type improvements (compare to

export interface StripeConfig {
).

What?

  • Changed the constructor config parameter type from Record<string, unknown> to StripeConfig across all entry points (stripe.core.ts, stripe.esm.node.ts, stripe.cjs.node.ts)
  • Added the missing authenticator property to StripeConfig (it was accepted at runtime via ALLOWED_CONFIG_PROPERTIES but absent from the public type)
  • Aligned UserProvidedConfig.port type to string | number to match StripeConfig.port
  • Typed Stripe.API_VERSION as typeof ApiVersion so it retains the literal type and is assignable to StripeConfig.apiVersion
  • Accepted StripeContext in StripeConfig.stripeContext to match UserProvidedConfig and runtime behavior
  • Added a compile-time type test asserting that unknown config properties are rejected

See Also

Changelog

  • Fixed some compile-time checks (no runtime changes)
    • Fixed Stripe constructor config parameter to use StripeConfig type instead of Record<string, unknown>, restoring compile-time type safety.
    • Added missing authenticator property to StripeConfig.
    • Fixed Stripe.API_VERSION to retain the literal API version type.
    • Fixed StripeConfig.stripeContext to accept StripeContext objects in addition to strings.

mbroshi-stripe and others added 2 commits April 7, 2026 12:54
The constructor's second parameter was typed as Record<string, unknown>,
which provided no compile-time type checking, autocomplete, or
documentation on hover. This changes it to use the existing StripeConfig
interface, giving TypeScript users proper type safety.

Also adds the missing `authenticator` property to StripeConfig (it was
accepted at runtime but absent from the public type) and aligns
UserProvidedConfig.port to accept string | number to match StripeConfig.

Fixes #2665 (partially — addresses the type safety complaint).

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Committed-By-Agent: claude
- Type `API_VERSION` as `typeof ApiVersion` to retain the literal type,
  so `Stripe.API_VERSION` is assignable to `StripeConfig.apiVersion`.
- Accept `StripeContext` in `StripeConfig.stripeContext` to match
  `UserProvidedConfig` and runtime behavior.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Committed-By-Agent: claude
@mbroshi-stripe mbroshi-stripe marked this pull request as ready for review April 7, 2026 20:36
@mbroshi-stripe mbroshi-stripe requested a review from a team as a code owner April 7, 2026 20:36
Copilot AI review requested due to automatic review settings April 7, 2026 20:37
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves TypeScript type-safety for the Stripe constructor config by switching from a permissive Record<string, unknown> to the public StripeConfig type, aligning runtime-accepted config options with the declared types, and adding a compile-time type test.

Changes:

  • Updated Stripe constructor config parameter types to StripeConfig (core + ESM Node + CJS Node wrapper) and refined internal config parsing typing.
  • Added authenticator to StripeConfig, widened stripeContext, and aligned UserProvidedConfig.port with StripeConfig.port.
  • Added a TypeScript compile-time test ensuring unknown constructor config properties are rejected.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
testProjects/types/typescriptTest.ts Adds a @ts-expect-error type test for unknown constructor config properties.
src/Types.ts Widens UserProvidedConfig.port to string | number.
src/stripe.esm.node.ts Uses StripeConfig for constructor config + types Stripe.API_VERSION as literal-preserving.
src/stripe.core.ts Uses StripeConfig for constructor config + types Stripe.API_VERSION as literal-preserving.
src/stripe.cjs.node.ts Updates the CJS callable wrapper signature to accept StripeConfig.
src/lib.ts Extends StripeConfig with authenticator and widens stripeContext to accept StripeContext.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/stripe.core.ts
Comment thread src/stripe.esm.node.ts
Comment thread src/Types.ts
Comment thread src/stripe.cjs.node.ts
Comment thread src/lib.ts
Missed this entry point in the initial change.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Committed-By-Agent: claude
Copy link
Copy Markdown
Member

@xavdid xavdid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good but I'd just make it clear in the title/changelog that nothing about the runtime behavior has changed: we used to have types for this function, we accidentally stop applying them, and now we're fixing that. Now they look like they did before the change:

export interface StripeConfig {

@mbroshi-stripe mbroshi-stripe changed the title Add type safety to Stripe constructor config Add type safety to Stripe constructor config (no runtime change) Apr 7, 2026
@mbroshi-stripe mbroshi-stripe merged commit c1ba099 into master Apr 8, 2026
10 checks passed
@mbroshi-stripe mbroshi-stripe deleted the mbroshi/type-safe-config branch April 8, 2026 00:09
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.

4 participants