web3/web3.js

Contract methods have parameters with the type `any[]`

Closed this issue · 2 comments

Expected behavior

Contract methods have typed parameters.

Actual behavior

Contract methods have parameters with the type any[].

Steps to reproduce the behavior

import Web3 from "web3";

const abi = [{
  inputs: [
    { internalType: "uint256", name: "testArg1", type: "uint256" },
    { internalType: "uint256", name: "testArg2", type: "uint256" },
  ],
  name: "test",
  outputs: [{ internalType: "uint256", name: "testRes1", type: "uint256" }],
  stateMutability: "nonpayable",
  type: "function",
}] as const;

const web3 = new Web3();
const contract = new web3.eth.Contract(abi);

// Actual: No error occurs here.
// Expected: Error - Expected 2 arguments, but got 0
contract.methods.test();

// Actual: any[]
// Expected: [MatchPrimitiveType<"uint256", unknown>, MatchPrimitiveType<"uint256", unknown>]
type Params = Parameters<typeof contract['methods']['test']>;

Solution

...args: Method['Inputs'] extends undefined | unknown ? any[] : Method['Inputs']

Should be changed to extends never or something similar. Since everything is extending type unknown.

Environment

web3: 4.13.0
typescript: 5.6.2

Hey @nicklatkovich , I'll have someone look into this soon.

Same happens for us. The methods are properly inferred, but the parameters are any[], which is very dangerous