score:7

Accepted answer

The error is caused by this line:

callback(error, null);

The type of error from catch (error) is unknown, and you specified that callback function accepts Error | null as its first parameter, hence why the error.

Read more here


Easy, but not recommended fix

Set strict value to false on your tsconfig file


Another easy, but better way

Explicitly specify the error type to any

try {
  const accounts = await web3.eth.getAccounts();
  callback(null, accounts[0]);
} catch (error: any) {
  callback(error, null);
}

Best way

Do a type checking inside the catch

try {
  const accounts = await web3.eth.getAccounts();
  callback(null, accounts[0]);
} catch (error) {
  if (error instanceof Error ) {
    callback(error, null);
  } else {
    // handle
  }
}

Related Query

More Query from same tag