uport-project / ethr-did

Create ethr DIDs
Apache License 2.0
259 stars 52 forks source link

[BUG][Typescript] Cannot assign type 'BaseProvider' to type 'Provider'. #113

Closed francesco-plt closed 1 year ago

francesco-plt commented 1 year ago

Current Behavior

const provider = getDefaultProvider(process.env.INFURA_PROJECT_ID, "sepolia");
const ethrDID = new EthrDID({ 
    privateKey,
    // @ts-ignore
    provider,
    registry: '...'
  });
Error: Cannot assign type 'BaseProvider' to type 'Provider'.

Expected Behavior

const provider = getDefaultProvider(process.env.INFURA_PROJECT_ID, "sepolia");
const ethrDID = new EthrDID({ 
    privateKey,
    // @ts-ignore
    provider,
    registry: '...'
  });

No compile errors, since the type of provider and the type expected by the constructor of EthrDID for provider should match.

Failure Information

I have not find a way to cast provider as of type Provider as defined by ethers. If I do the following:

import { Provider } from '@ethersproject/providers'
const provider: Provider = getDefaultProvider(process.env.INFURA_PROJECT_ID, "sepolia");
const ethrDID = new EthrDID({ 
    privateKey,
    // @ts-ignore
    provider,
    registry: '...'
  });

Then I get the following error:

Type 'import("node_modules/@ethersproject/abstract-provider/lib/index").Provider' cannot be assigned to type 'import("node_modules/ethr-did/node_modules/@ethersproject/abstract-provider/lib/index").Provider'.
Types returned by 'getFeeData()' are not compatible.
Type 'Promise<import("node_modules/@ethersproject/abstract-provider/lib/index").FeeData>' cannot be assigned to type 'Promise<import("node_modules/ethr-did/node_modules/@ethersproject/abstract-provider/lib/index").FeeData>'.
Property 'lastBaseFeePerGas' is missing in type 'import("node_modules/@ethersproject/abstract-provider/lib/index").FeeData', but it's mandatory in type 'import("node_modules/ethr-did/node_modules/@ethersproject/abstract-provider/lib/index").FeeData'.
mirceanis commented 1 year ago

This is likely coming from a difference in versions of some ethers packages.

The current version of ethr-did depends on ethers@5.7.x (or rather some @ethersproject/* packages) You are probably using some other version that is causing the difference in types.

The // @ts-ignore you are already using should be enough as a workaround. An alternative is to cast provider to any:

const provider = getDefaultProvider(process.env.INFURA_PROJECT_ID, "sepolia");
const ethrDID = new EthrDID({ 
    privateKey,
    provider as any, // casting to any should convince typescript that it's fine.
    registry: '...'
  });
francesco-plt commented 1 year ago

Thanks, using version 5.7.2 and importing providers.Provider fixed it.