fix(types): constrain AxiosHeaders custom fields by LaplaceYoung · Pull Request #10941 · axios/axios · GitHub
Skip to content

fix(types): constrain AxiosHeaders custom fields#10941

Open
LaplaceYoung wants to merge 2 commits into
axios:v1.xfrom
LaplaceYoung:fix/axiosheaders-index-signature
Open

fix(types): constrain AxiosHeaders custom fields#10941
LaplaceYoung wants to merge 2 commits into
axios:v1.xfrom
LaplaceYoung:fix/axiosheaders-index-signature

Conversation

@LaplaceYoung

@LaplaceYoung LaplaceYoung commented May 24, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Tighten the AxiosHeaders string index signature to accepted header values or declared methods in both ESM and CJS typings.
  • Add module typing coverage for valid custom header fields and a rejected Promise assignment.
  • Runtime behavior remains unchanged.

Fixes #7487

Tests

  • .\node_modules\.bin\mocha --timeout 10000 tests\typings.module.test.cjs (from tests/module/cjs)
  • npx vitest run --config vitest.config.js --project module tests/typings.module.test.js (from tests/module/esm)
  • npx prettier --check index.d.ts index.d.cts tests/module/esm/tests/typings.module.test.js tests/module/cjs/tests/typings.module.test.cjs tests/module/esm/tests/helpers/headers-types.ts tests/module/cjs/tests/helpers/headers-types.ts
  • git diff --check

Summary by cubic

Constrain AxiosHeaders string index to valid header values while preserving declared method calls across ESM and CJS. Branded class methods so custom function fields are rejected; type-only change, no runtime impact.

Description

  • Branded declared AxiosHeaders methods and typed them as properties to distinguish them from arbitrary functions.
  • Narrowed [key: string] to AxiosHeaderValueOrMethod in index.d.ts and index.d.cts so custom fields accept only header values; methods remain callable.
  • Added ESM/CJS type tests for custom fields: allow string/number/array; reject Promise and custom function assignments.
  • Docs: update /docs/ to list allowed custom header value types and note that promises/functions are not allowed.
  • Fixes typings bug (not type safety): class field type of AxiosHeaders => any #7487.

Semantic version impact

  • Patch: Type tightening only; may surface invalid TypeScript usages at compile time.

Written for commit d31a7a0. Summary will update on new commits. Review in cubic

Narrow custom header field assignments to values axios accepts while keeping declared header methods callable.

Constraint: Preserve declared AxiosHeaders methods while narrowing arbitrary string fields.
Rejected: Keep the any index signature | It allows non-header values to type-check.
Confidence: high
Scope-risk: narrow
Directive: Keep ESM and CJS declarations in sync for AxiosHeaders type changes.
Tested: .\\node_modules\\.bin\\mocha --timeout 10000 tests\\typings.module.test.cjs (from tests/module/cjs)
Tested: npx vitest run --config vitest.config.js --project module tests/typings.module.test.js (from tests/module/esm)
Tested: npx prettier --check changed typing files
Tested: git diff --check
Not-tested: full test suite

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 6 files

Confidence score: 4/5

  • This PR looks safe to merge with minimal risk: the reported issue is moderate (5/10) and focused on type constraints rather than a clear runtime break.
  • In index.d.cts, the AxiosHeaders string index still allows arbitrary function values, so custom header fields are not fully restricted to valid header value types.
  • The impact appears to be mostly TypeScript typing looseness (potentially weaker compile-time safety) rather than a concrete user-facing regression.
  • Pay close attention to index.d.cts - tighten the AxiosHeaders index signature so custom headers cannot accept arbitrary functions.
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:13">
P2: The new `AxiosHeaders` string index type still permits arbitrary function values, so custom header fields are not fully constrained to header values.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread index.d.cts Outdated
Brand declared AxiosHeaders methods so the string index can accept the class API while custom header fields remain limited to AxiosHeaderValue.

Constraint: The AxiosHeaders class needs a string index for custom headers and string-named methods on the same instance.
Rejected: Keep a broad function index signature | It lets arbitrary custom header functions type-check.
Confidence: high
Scope-risk: narrow
Directive: Keep ESM and CJS AxiosHeaders declarations aligned.
Tested: .\node_modules\.bin\mocha --timeout 10000 tests\typings.module.test.cjs (from tests/module/cjs)
Tested: npx vitest run --config vitest.config.js --project module tests/typings.module.test.js (from tests/module/esm)
Tested: npx prettier --check index.d.ts index.d.cts tests/module/cjs/tests/helpers/headers-types.ts tests/module/esm/tests/helpers/headers-types.ts tests/module/cjs/tests/typings.module.test.cjs tests/module/esm/tests/typings.module.test.js
Tested: git diff --check
Not-tested: full test suite
@LaplaceYoung

Copy link
Copy Markdown
Contributor Author

@jasonsaayman jasonsaayman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We may need to hold off on this merge, but let's make these changes in the meantime. This would be a breaking change if it lands.

Comment thread index.d.ts
export class AxiosHeaders {
constructor(headers?: RawAxiosHeaders | AxiosHeaders | string);

[key: string]: AxiosHeaderValueOrMethod;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Generated custom accessors from AxiosHeaders.accessor('X-Foo') are no longer callable by type because unknown keys resolve to AxiosHeaderValueOrMethod, a union with non-callable members. Add ESM and CJS type tests for setXFoo / getXFoo / hasXFoo, then preserve callable typings, likely with template-literal signatures for set${string}, get${string}, and has${string}.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Add positive assertions that declared methods still call correctly, not only negative custom-field assignments.

@jasonsaayman jasonsaayman added the status::changes-requested A reviewer requested changes to the PR label May 27, 2026
@jasonsaayman jasonsaayman added status::blocked This PR or issue is deemed to be blocked status::stale This PR or issue is deemed stale. Feedback has not been provided in 2 weeks. type::breaking The PR introduces breaking changes commit::fix The PR is related to a bugfix and removed priority::medium labels Jun 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit::fix The PR is related to a bugfix status::blocked This PR or issue is deemed to be blocked status::changes-requested A reviewer requested changes to the PR status::stale This PR or issue is deemed stale. Feedback has not been provided in 2 weeks. type::breaking The PR introduces breaking changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

typings bug (not type safety): class field type of AxiosHeaders => any

2 participants