Skip to content

fix(types): align runWhen type with runtime behavior in InterceptorManager#7529

Open
techcodie wants to merge 1 commit intoaxios:v1.xfrom
techcodie:fix/runwhen-type-mismatch
Open

fix(types): align runWhen type with runtime behavior in InterceptorManager#7529
techcodie wants to merge 1 commit intoaxios:v1.xfrom
techcodie:fix/runwhen-type-mismatch

Conversation

@techcodie
Copy link
Contributor

@techcodie techcodie commented Mar 19, 2026

Summary

Fixes #7404

The runWhen property in AxiosInterceptorOptions and AxiosInterceptorHandler had type inconsistencies that caused TypeScript build failures when upgrading from axios 1.13.2 to 1.13.5+.

Problems Fixed

1. AxiosInterceptorOptions.runWhen did not allow null

InterceptorManager.use() explicitly sets runWhen to null when no options are provided:

// lib/core/InterceptorManager.js:24
runWhen: options ? options.runWhen : null,

But the type definition did not reflect this:

// Before
runWhen?: (config: InternalAxiosRequestConfig) => boolean;
// After
runWhen?: ((config: InternalAxiosRequestConfig) => boolean) | null;

2. AxiosInterceptorHandler.runWhen used wrong config type

The handler type used AxiosRequestConfig while AxiosInterceptorOptions used InternalAxiosRequestConfig. Since InterceptorManager receives its runWhen directly from the options, both should use InternalAxiosRequestConfig.

3. Incorrect null placement in index.d.ts

In index.d.ts, the type was:

runWhen: (config: AxiosRequestConfig) => boolean | null;

This means "a required function that returns boolean | null", but the actual runtime value is "a function OR null":

runWhen?: ((config: InternalAxiosRequestConfig) => boolean) | null;

4. AxiosInterceptorHandler.runWhen now allows undefined

When options is provided without a runWhen property, options.runWhen evaluates to undefined. The type is now optional (?) to allow both null (no options passed) and undefined (options passed without runWhen).

Files Changed

File Change
index.d.ts Fixed AxiosInterceptorOptions.runWhen and AxiosInterceptorHandler.runWhen types
index.d.cts Same fixes for CJS type definitions

How It Was Verified

  • Confirmed the runtime behavior in lib/core/InterceptorManager.js (line 24) sets runWhen to null
  • Confirmed lib/core/Axios.js (line 145) checks typeof interceptor.runWhen === 'function', safely handling null and undefined
  • Changes are backward compatible — existing code using runWhen as a function continues to work

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 2 files

Confidence score: 4/5

  • This PR looks safe to merge overall, with one moderate typing mismatch rather than a clear runtime break.
  • In index.d.cts, AxiosInterceptorHandler.runWhen is typed too narrowly; at runtime it can be undefined when interceptor options are passed without runWhen, which can cause TypeScript consumers to see incorrect type expectations.
  • Given the issue is medium severity (5/10) and limited to declaration typing, risk appears contained but worth a follow-up fix to keep typings aligned with runtime behavior.
  • Pay close attention to index.d.cts - ensure runWhen allows undefined to match actual runtime assignment.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="index.d.cts">

<violation number="1" location="index.d.cts:652">
P2: `AxiosInterceptorHandler.runWhen` type is too narrow: runtime can set it to `undefined` when options are provided without `runWhen`.</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

…nager

The `runWhen` property in `AxiosInterceptorOptions` and
`AxiosInterceptorHandler` had type inconsistencies that caused
TypeScript build failures when upgrading from axios 1.13.2 to 1.13.5+.

Issues fixed:

1. `AxiosInterceptorOptions.runWhen` did not allow `null`, but
   `InterceptorManager.use()` explicitly sets it to `null` when no
   options are provided (`runWhen: options ? options.runWhen : null`).

2. `AxiosInterceptorHandler.runWhen` used `AxiosRequestConfig` instead
   of `InternalAxiosRequestConfig`, creating a type mismatch with
   `AxiosInterceptorOptions` which uses `InternalAxiosRequestConfig`.

3. In `index.d.ts`, the `AxiosInterceptorHandler.runWhen` type was
   `(config: AxiosRequestConfig) => boolean | null` — the `| null`
   was incorrectly placed as part of the return type rather than making
   the entire function type nullable.

4. `AxiosInterceptorHandler.runWhen` is now optional (`?`) to also
   allow `undefined`, matching the runtime case where options are
   passed without a `runWhen` property (i.e. `options.runWhen` is
   `undefined`).

Fixes axios#7404
@techcodie techcodie force-pushed the fix/runwhen-type-mismatch branch from 6951273 to bcddc76 Compare March 19, 2026 18:55
@techcodie techcodie changed the title fix(types): align runWhen type with runtime behavior in InterceptorMa… fix(types): align runWhen type with runtime behavior in InterceptorManager Mar 19, 2026
@techcodie
Copy link
Contributor Author

techcodie commented Mar 19, 2026

Addressed the review feedback — runWhen in AxiosInterceptorHandler is now optional (?) to also allow undefined, covering all runtime states: Function, null, and undefined.

Changes are backward compatible and fix the TypeScript build failures reported in #7404. This PR is ready to merge.

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.

issue in runWhen types

1 participant