fix(types): constrain AxiosHeaders custom fields#10941
fix(types): constrain AxiosHeaders custom fields#10941LaplaceYoung wants to merge 2 commits intoaxios:v1.xaxios/axios:v1.xfrom LaplaceYoung:fix/axiosheaders-index-signatureLaplaceYoung/axios:fix/axiosheaders-index-signatureCopy head branch name to clipboard
Conversation
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
There was a problem hiding this comment.
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, theAxiosHeadersstring 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 theAxiosHeadersindex 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
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
|
Updated in d31a7a0. The AxiosHeaders index now accepts only header values plus branded declared method types, so custom header fields reject function assignments while the existing AxiosHeaders methods remain callable. Added CJS and ESM type fixtures covering function assignment with Checked:
|
jasonsaayman
left a comment
There was a problem hiding this comment.
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.
| export class AxiosHeaders { | ||
| constructor(headers?: RawAxiosHeaders | AxiosHeaders | string); | ||
|
|
||
| [key: string]: AxiosHeaderValueOrMethod; |
There was a problem hiding this comment.
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}.
| @@ -0,0 +1,13 @@ | ||
| import { AxiosHeaders } from '../../../../index.js'; | ||
|
|
||
| const headers = new AxiosHeaders({ x: 1 }); |
There was a problem hiding this comment.
Add positive assertions that declared methods still call correctly, not only negative custom-field assignments.
Summary
Fixes #7487
Tests
Summary by cubic
Constrain
AxiosHeadersstring 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
AxiosHeadersmethods and typed them as properties to distinguish them from arbitrary functions.[key: string]toAxiosHeaderValueOrMethodinindex.d.tsandindex.d.ctsso custom fields accept only header values; methods remain callable.Promiseand custom function assignments./docs/to list allowed custom header value types and note that promises/functions are not allowed.Semantic version impact
Written for commit d31a7a0. Summary will update on new commits. Review in cubic