[typescript-axios] Fix generated package build on TypeScript 6 (types node, @types/node 20, ES5 range)
Fixes #25598 (regression introduced by #25148, shipped in 7.27.0).
Since 7.27.0 a typescript-axios client generated with npmName fails its npm install, because the prepare script now compiles with TypeScript 6 and the generated @types/node is never loaded (TypeScript 6 defaults types to an empty list):
api.ts(55,51): error TS2550: Property 'entries' does not exist on type 'ObjectConstructor'.
common.ts(19,38): error TS2591: Cannot find name 'url'. Do you need to install type definitions for node?
Changes
tsconfig.mustache: list"node"intypesexplicitly.package.mustache: bump@types/nodefrom12.11.5 - 12.20.42to^20.0.0.@types/node12 is incompatible with the TypeScript 6 standard library (DateTimeRangeFormatPart), and@types/node>= 16 needs TypeScript >= 4.9, so the lower bound of thetypescriptrange follows (^4.9).package.mustache: only include^6.0in thetypescriptrange whensupportsES6is set. TypeScript 6 rejectstarget: ES5unlessignoreDeprecationsis"6.0", and TypeScript 5 rejects that value, so one generatedtsconfig.jsoncannot serve both compilers for the ES5 target.- Samples: removed the hand-written
custom.d.ts(declare module 'url';) from the petstore builds. It dates from the 2018 scaffold (#892), is not generated, and turnsurlinto a shorthand ambient module, which breaks the@types/node20urltypings (Cannot use namespace '_URL' as a type). Refreshed the three trackedpackage-lock.jsonfiles for the new ranges. TypeScriptAxiosClientCodegenTest: extended the TypeScript 6 test and added one for the ES5 range.
Verification
Compatibility matrix for the generated ES6 client with types: ["node"] (0 errors unless noted):
@types/node | TS 4.9.5 | TS 5.0.4 | TS 5.9.3 | TS 6.0.3 |
|---|---|---|---|---|
| 16 / 18 / 20 | ok | ok | ok | ok |
| 22 | fails | fails | ok | ok |
| 24 | fails | fails | ok | ok |
@types/node 18 and 20 fail on TypeScript 4.0 and 4.6 (syntax errors in the typings), hence ^4.9.
npm install(which runsprepare->tsc && tsc -p tsconfig.esm.json) succeeds in all regenerated samples with apackage.json:es6-target(TypeScript 6.0.3 resolved,dist/anddist/esm/),with-npm-version(5.9.3 from the refreshed lock),with-npm-version-and-separate-models-and-api(5.9.3),with-complex-headers(5.6.3),echo_api/build(5.6.3).- The minimal spec from #25598 (object query parameter +
withNodeImports=true) compiles with TypeScript 6.0.3 after this change. ./bin/generate-samples.sh ./bin/configs/typescript-axios-*.yamlregenerated the samples; onlypackage.jsonandtsconfig.jsonchanged../mvnw -pl modules/openapi-generator -am -Dtest=TypeScriptAxiosClientCodegenTest -Dsurefire.failIfNoSpecifiedTests=false test: 16 tests, 0 failures.
Note: this branch is based on 0a5b61b1 because current master fails to compile (CSharpClientCodegen.postProcessAllModels is defined twice since #25593).
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples:
Only the./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exittypescript-axiosconfigs were regenerated; the change touches no other generator. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka @joscha @KannaKim
Summary by cubic
Fixes generated typescript-axios clients failing npm install when the prepare script resolves TypeScript 6, which no longer loads @types/node by default.
Bug Fixes
- Lists
"node"explicitly in the generatedtsconfig.jsontypessoObject.entriesand theurlimport compile under TypeScript 6. - Bumps
@types/nodefrom12.11.5 - 12.20.42to^20.0.0and raises the TypeScript range lower bound to^4.9. - Excludes TypeScript 6 from the range for the ES5 target, since neither compiler accepts the other's
ignoreDeprecationsconfiguration fortarget: ES5. - Removes the hand-written
declare module 'url'shim from petstore samples, which shadowed the@types/node20urltypings. - Refreshes the affected sample
package.json,tsconfig.json, andpackage-lock.jsonfiles and extends the codegen tests to cover both ranges.