From 953c83911e9024bdd97bedfbcd489145eed4eb61 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Wed, 6 Jul 2022 13:18:53 +0200 Subject: [PATCH] :label: Fix the contract types --- packages/web3-eth-abi/src/types.ts | 24 +++++++++++-------- packages/web3-eth-contract/src/contract.ts | 2 +- .../test/unit/contract_typing.test.ts | 6 ++--- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/web3-eth-abi/src/types.ts b/packages/web3-eth-abi/src/types.ts index 08a318a05ed..2b6c5141127 100644 --- a/packages/web3-eth-abi/src/types.ts +++ b/packages/web3-eth-abi/src/types.ts @@ -177,38 +177,42 @@ export type MatchPrimitiveType< | PrimitiveTupleType | never; -export type ContractMethodOutputParameters | undefined> = - Params extends [] +export type ContractMethodOutputParameters | undefined> = + Params extends readonly [] ? [] - : Params extends [infer H, ...infer R] + : Params extends readonly [infer H, ...infer R] ? H extends AbiParameter ? // TODO: Find a way to set name for tuple item [MatchPrimitiveType, ...ContractMethodOutputParameters] : ContractMethodOutputParameters + : Params extends undefined | unknown + ? [] : Params; -export type ContractMethodInputParameters | undefined> = - Params extends [] +export type ContractMethodInputParameters | undefined> = + Params extends readonly [] ? [] - : Params extends [infer H, ...infer R] + : Params extends readonly [infer H, ...infer R] ? H extends AbiParameter ? // TODO: Find a way to set name for tuple item [MatchPrimitiveType, ...ContractMethodInputParameters] : ContractMethodInputParameters + : Params extends undefined | unknown + ? [] : Params; export type ContractConstructor = { [Abi in FilterAbis as 'constructor']: { readonly Abi: Abi; - readonly Inputs: ContractMethodInputParameters<[...NonNullable]>; + readonly Inputs: ContractMethodInputParameters; }; }['constructor']; export type ContractMethod = { readonly Abi: Abi; - readonly Inputs?: ContractMethodInputParameters<[...NonNullable]>; - readonly Outputs?: ContractMethodOutputParameters<[...NonNullable]>; + readonly Inputs: ContractMethodInputParameters; + readonly Outputs: ContractMethodOutputParameters; }; export type ContractMethods = { @@ -220,7 +224,7 @@ export type ContractMethods = { export type ContractEvent = { readonly Abi: Abi; - readonly Inputs: ContractMethodInputParameters<[...NonNullable]>; + readonly Inputs: ContractMethodInputParameters; }; export type ContractEvents = { diff --git a/packages/web3-eth-contract/src/contract.ts b/packages/web3-eth-contract/src/contract.ts index d3f5464c41a..61db8b6a0cc 100644 --- a/packages/web3-eth-contract/src/contract.ts +++ b/packages/web3-eth-contract/src/contract.ts @@ -92,7 +92,7 @@ import { // eslint-disable-next-line @typescript-eslint/no-explicit-any type ContractBoundMethod> = ( - ...args: NonNullable + ...args: Method['Inputs'] ) => Method['Abi']['stateMutability'] extends 'payable' | 'pure' ? PayableMethodObject : NonPayableMethodObject; diff --git a/packages/web3-eth-contract/test/unit/contract_typing.test.ts b/packages/web3-eth-contract/test/unit/contract_typing.test.ts index 781155a5f15..58b0e69dae9 100644 --- a/packages/web3-eth-contract/test/unit/contract_typing.test.ts +++ b/packages/web3-eth-contract/test/unit/contract_typing.test.ts @@ -30,9 +30,9 @@ describe('contract typing', () => { expectTypeOf().toBe(), ); - // typecheck.skip('should have interface compliance methods', () => - // expectTypeOf(contract.methods).toExtend(), - // ); + typecheck('should have interface compliance methods', () => + expectTypeOf(contract.methods).toExtend(), + ); typecheck('should have all events', () => expectTypeOf().toBe(),