Skip to content

Commit

Permalink
Align types with @metamask/eth-json-rpc-provider (#21)
Browse files Browse the repository at this point in the history
Currently, passing an instance of SafeEventEmitterProvider to the
EthQuery constructor produces a type error. This is happening because
the `Provider` type that this package defines allows the `params`
property of the JSON-RPC request object to be anything, whereas in the
SafeEventEmitterProvider interface, it has to be JSON-compatible.
In other words, the provider that this package expects isn't compliant
with a "proper" provider.

To fix this, this commit manually copies the `Json` and `JsonRpcParams`
types from `@metamask/utils` to avoid a direct dependency. This is a
temporary solution to prevent this type error from spreading within
the `core` codebase. We will investigate a more overarching solution
later.
  • Loading branch information
mcmire authored Nov 9, 2023
1 parent 948eaba commit 4f1dc24
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ declare module '@metamask/eth-query' {
| symbol
| undefined;

type Json =
| null
| boolean
| number
| string
| Json[]
| { [prop: string]: Json };

type JsonRpcParams = Json[] | Record<string, Json>;

type ProviderSendAsyncResponse<Result> = {
error?: { message: string };
result?: Result;
Expand All @@ -21,13 +31,13 @@ declare module '@metamask/eth-query' {
) => void;

type Provider = {
sendAsync<Params, Result>(
sendAsync<Params extends JsonRpcParams, Result>(
payload: SendAsyncPayload<Params>,
callback: ProviderSendAsyncCallback<Result>,
): void;
};

type SendAsyncPayload<Params> = {
type SendAsyncPayload<Params extends JsonRpcParams> = {
id: number;
jsonrpc: '2.0';
method: string;
Expand All @@ -43,7 +53,7 @@ declare module '@metamask/eth-query' {
export default class EthQuery {
constructor(provider: Provider);

sendAsync<Params, Result>(
sendAsync<Params extends JsonRpcParams, Result>(
opts: Partial<SendAsyncPayload<Params>>,
callback: SendAsyncCallback<Result>,
): void;
Expand Down

0 comments on commit 4f1dc24

Please sign in to comment.