core icon indicating copy to clipboard operation
core copied to clipboard

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

Open MajorLift opened this issue 2 years ago • 0 comments

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
    • [ ] Initial notes added here: https://github.com/MetaMask/contributor-docs/issues/69#issuecomment-1910430960
  • [ ] Look into adding eslint rules for avoiding return type-only generics.

MajorLift avatar Nov 08 '23 17:11 MajorLift