Skip to content

Commit 4a63eea

Browse files
committed
docs: Explain the typecasts in provider-from-middleware
1 parent 3af776f commit 4a63eea

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

packages/eth-json-rpc-provider/src/provider-from-middleware.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export function providerFromMiddleware<
2424
Result extends Json,
2525
>(middleware: LegacyJsonRpcMiddleware<Params, Result>): InternalProvider {
2626
return providerFromMiddlewareV2(
27+
// This function is generic on the Params and Result types to match the legacy JsonRpcMiddleware type.
28+
// However, since the V2 JsonRpcMiddleware type is not generic on the Params, we need to elide this
29+
// parameter by upcasting the request type to JsonRpcRequest, or we get an error due to contravariance
30+
// since JsonRpcRequest<Params> is not assignable to JsonRpcRequest.
2731
asV2Middleware(middleware) as JsonRpcMiddleware<JsonRpcRequest>,
2832
);
2933
}
@@ -44,6 +48,9 @@ export function providerFromMiddlewareV2<
4448
>(middleware: Middleware): InternalProvider {
4549
return new InternalProvider({
4650
engine: JsonRpcEngineV2.create({
51+
// This function is generic in order to accept middleware functions with narrower types than
52+
// the plain JsonRpcMiddleware<JsonRpcRequest> type. However, since InternalProvider is non-generic,
53+
// we need to upcast the middleware to avoid a type error.
4754
middleware: [middleware as JsonRpcMiddleware<JsonRpcRequest>],
4855
}),
4956
});

0 commit comments

Comments
 (0)