fix(typescript-fetch): type FetchError cause without parameter property

TH
5h ago
+217-26

With lib es2022 Error already has cause, so parameter property constructor(public cause: Error, ...) in FetchError gives TS4115 under noImplicitOverride. Adding override is not an option, it failes with lib below es2022 (TS4113)

Now cause is assigned in constructor and typed through interface merged with the class. declare cause: Error in class body also compiles, but Babel 7 rejects declare fields unless allowDeclareFields is on, so went with interface (same name is needed for merging, this is the question from #24575). Only FetchError is changed, other parameter properties left as they are

Checked generated runtime.ts with tsc 5.0, 5.9 and 6.0, --strict --noImplicitOverride, lib es2022 and es2015: no errors, master has TS4115 on es2022. e.cause is still typed as Error. Added test in TypeScriptFetchClientCodegenTest, typescript tests pass. Regenerated all 93 typescript configs, only 25 typescript-fetch runtime.ts change, same hunk in each

Fixes #14515

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    Commit all changed files.
  • 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 the TypeScript 4115 compiler error in typescript-fetch's generated FetchError when noImplicitOverride is enabled and the lib is es2022, where Error already declares cause.

Bug Fixes

  • cause is now assigned in the constructor instead of using a parameter property, since override fails with lib es2015 and declare fields are rejected by Babel 7.
  • cause is typed via a declaration-merged interface and stays typed as Error; verified with tsc 5.0, 5.9, and 6.0 under --strict --noImplicitOverride with es2015 and es2022 libs, and all 25 typescript-fetch samples were regenerated.

Fixes #14515