11import { assertType , describe , expectTypeOf , test } from 'vitest'
22
3- import type { Abi } from './abi.js'
3+ import type { Abi , AbiParameterKind } from './abi.js'
44import type {
55 customSolidityErrorsAbi ,
66 ensRegistryWithFallbackAbi ,
@@ -424,36 +424,63 @@ describe('AbiParameterToPrimitiveType', () => {
424424
425425describe ( 'AbiParametersToPrimitiveTypes' , ( ) => {
426426 test ( 'no parameters' , ( ) => {
427- type Result = AbiParametersToPrimitiveTypes < [ ] >
428- assertType < Result > ( [ ] )
427+ type r1 = AbiParametersToPrimitiveTypes < [ ] >
428+ assertType < r1 > ( [ ] )
429+ type r2 = AbiParametersToPrimitiveTypes < [ ] , AbiParameterKind , true >
430+ assertType < r2 > ( [ ] )
429431 } )
430432
431433 test ( 'single parameter' , ( ) => {
432- type Result = AbiParametersToPrimitiveTypes <
434+ type r1 = AbiParametersToPrimitiveTypes <
433435 [
434436 {
435437 name : 'tokenId'
436438 type : 'uint8[2]'
437439 } ,
438440 ]
439441 >
440- assertType < Result > ( [ [ 1 , 1 ] ] )
442+ assertType < r1 > ( [ [ 1 , 1 ] ] )
441443 // ^?
444+ type r2 = AbiParametersToPrimitiveTypes <
445+ [
446+ {
447+ name : 'tokenId'
448+ type : 'uint8[2]'
449+ } ,
450+ ] ,
451+ AbiParameterKind ,
452+ true
453+ >
454+ expectTypeOf < r2 > ( ) . toEqualTypeOf <
455+ readonly [ tokenId : readonly [ number , number ] ]
456+ > ( )
442457 } )
443458
444459 test ( 'multiple parameters' , ( ) => {
445- type Result = AbiParametersToPrimitiveTypes <
460+ type r1 = AbiParametersToPrimitiveTypes <
446461 [
447462 { name : 'to' ; type : 'address' } ,
448463 { name : 'tokenId' ; type : 'uint256' } ,
449464 { name : 'trait' ; type : 'string[]' } ,
450465 ]
451466 >
452- assertType < Result > ( [ zeroAddress , 1n , [ 'foo' ] ] )
467+ assertType < r1 > ( [ zeroAddress , 1n , [ 'foo' ] ] )
468+ type r2 = AbiParametersToPrimitiveTypes <
469+ [
470+ { name : 'to' ; type : 'address' } ,
471+ { name : 'tokenId' ; type : 'uint256' } ,
472+ { name : 'trait' ; type : 'string[]' } ,
473+ ] ,
474+ AbiParameterKind ,
475+ true
476+ >
477+ expectTypeOf < r2 > ( ) . toEqualTypeOf <
478+ readonly [ to : `0x${string } `, tokenId : bigint , readonly string [ ] ]
479+ > ( )
453480 } )
454481
455482 test ( 'deeply nested parameters' , ( ) => {
456- type Result = AbiParametersToPrimitiveTypes <
483+ type r1 = AbiParametersToPrimitiveTypes <
457484 [
458485 {
459486 name : 's'
@@ -490,7 +517,55 @@ describe('AbiParametersToPrimitiveTypes', () => {
490517 } ,
491518 ]
492519 >
493- assertType < Result > ( [
520+ assertType < r1 > ( [
521+ { a : 1 , b : [ 2 ] , c : [ { x : 1 , y : 1 } ] } ,
522+ { x : 1 , y : 1 } ,
523+ 1 ,
524+ [
525+ { x : 1n , y : 1n } ,
526+ { x : 1n , y : 1n } ,
527+ ] ,
528+ ] )
529+ type r2 = AbiParametersToPrimitiveTypes <
530+ [
531+ {
532+ name : 's'
533+ type : 'tuple'
534+ components : [
535+ { name : 'a' ; type : 'uint8' } ,
536+ { name : 'b' ; type : 'uint8[]' } ,
537+ {
538+ name : 'c'
539+ type : 'tuple[]'
540+ components : [
541+ { name : 'x' ; type : 'uint8' } ,
542+ { name : 'y' ; type : 'uint8' } ,
543+ ]
544+ } ,
545+ ]
546+ } ,
547+ {
548+ name : 't'
549+ type : 'tuple'
550+ components : [
551+ { name : 'x' ; type : 'uint8' } ,
552+ { name : 'y' ; type : 'uint8' } ,
553+ ]
554+ } ,
555+ { name : 'a' ; type : 'uint8' } ,
556+ {
557+ name : 't'
558+ type : 'tuple[2]'
559+ components : [
560+ { name : 'x' ; type : 'uint256' } ,
561+ { name : 'y' ; type : 'uint256' } ,
562+ ]
563+ } ,
564+ ] ,
565+ AbiParameterKind ,
566+ true
567+ >
568+ assertType < r2 > ( [
494569 { a : 1 , b : [ 2 ] , c : [ { x : 1 , y : 1 } ] } ,
495570 { x : 1 , y : 1 } ,
496571 1 ,
@@ -514,14 +589,20 @@ describe('AbiParametersToPrimitiveTypes', () => {
514589 assertType < AbiParametersToPrimitiveTypes < typeof parameters , 'outputs' > > ( [
515590 '0xfoo' ,
516591 ] )
517- assertType < AbiParametersToPrimitiveTypes < typeof parameters , 'outputs' > > ( [
518- '0xfoo' ,
519- ] )
592+ assertType <
593+ AbiParametersToPrimitiveTypes < typeof parameters , 'inputs' , true >
594+ > ( [ '0xfoo' ] )
595+ assertType <
596+ AbiParametersToPrimitiveTypes < typeof parameters , 'outputs' , true >
597+ > ( [ '0xfoo' ] )
520598 } )
521599
522600 test ( 'named parameters' , ( ) => {
523601 type Result = AbiParametersToPrimitiveTypes <
524- ExtractAbiFunction < typeof erc20Abi , 'transferFrom' > [ 'inputs' ]
602+ // ^?
603+ ExtractAbiFunction < typeof erc20Abi , 'transferFrom' > [ 'inputs' ] ,
604+ 'inputs' ,
605+ true
525606 >
526607 expectTypeOf < Result > ( ) . toEqualTypeOf <
527608 readonly [ sender : `0x${string } `, recipient : `0x${string } `, amount : bigint ]
0 commit comments