MetaMask / MetaMask/core

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

Open
#2,010 0 comments 0 reactions 1 assignee Claimed by @MajorLift View on GitHub
team-assets team-wallet-framework wf-controller-tech-debt
Dominant language
TypeScript
Stars
413
Forks
308
Avg merge
1d 4h
Merged PRs (30d)
253

Description

## Motivation

- Middleware functions in `eth-json-rpc-middleware`, `eth-json-rpc-engine`, `eth-json-rpc-infura` are ["return type-only" generics](https://google.github.io/styleguide/tsguide.html#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` is not assignable to parameter of type `JsonRpcMiddleware`"

```ts
// before
function createExampleMiddleware(exampleParam): JsonRpcMiddleware
// after
function createExampleMiddleware<
Params extends JsonRpcParams = JsonRpcParmas,
Result extends Json = Json
>(exampleParam): JsonRpcMiddleware
```

## 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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.