Skip to content

Fix middleware functions with implicit, return type-only generic parameters #2010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
6 tasks
MajorLift opened this issue Nov 8, 2023 · 0 comments
Open
6 tasks
Assignees

Comments

@MajorLift
Copy link
Contributor

MajorLift commented Nov 8, 2023

Motivation

  • Middleware functions in eth-json-rpc-middleware, eth-json-rpc-engine, eth-json-rpc-infura are "return type-only" generics with implicit generic parameters. This is a discouraged pattern.
  • Redefining the functions to expose the generic parameters that are implicitly hard-coded into their return type would make them less brittle to future typing updates.
    • Example error: "JsonRpcMiddleware<unknown, unknown> is not assignable to parameter of type JsonRpcMiddleware<JsonRpcParams, Json>"
// before
function createExampleMiddleware(exampleParam): JsonRpcMiddleware<JsonRpcParams, Json>
// after
function createExampleMiddleware<
  Params extends JsonRpcParams = JsonRpcParmas, 
  Result extends Json = Json
>(exampleParam): JsonRpcMiddleware<Params, Result>

Tasks

  • Compile list of functions and types to be fixed.
  • Expose the return type generic params in the outer type/function.
    • Assign default arguments to provide a consistent interface and minimize disruption.
  • Add contributor-docs TypeScript style-guide entry
  • Look into adding eslint rules for avoiding return type-only generics.
@MajorLift MajorLift self-assigned this Nov 8, 2023
@MajorLift MajorLift changed the title Improve typing for middleware functions Fix middleware functions with implicit, return type-only generic parameters Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant