Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): payable value inference ts value #2307

Merged
merged 1 commit into from
May 24, 2024
Merged

fix(types): payable value inference ts value #2307

merged 1 commit into from
May 24, 2024

Conversation

tmm
Copy link
Member

@tmm tmm commented May 24, 2024

Upstream issue in wagmi and @wagmi/vue with payable value property.

wevm/wagmi#3984


PR-Codex overview

This PR improves type inference for the value property in contracts and updates test cases for simulateContract.

Detailed summary

  • Fixed type inference for value property in contracts
  • Added NoInfer type utility
  • Updated test cases for simulateContract with value property

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Copy link

vercel bot commented May 24, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
viem ✅ Ready (Inspect) Visit Preview May 24, 2024 8:01pm

Copy link

changeset-bot bot commented May 24, 2024

🦋 Changeset detected

Latest commit: db22244

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@tmm tmm marked this pull request as ready for review May 24, 2024 19:54
Copy link
Contributor

github-actions bot commented May 24, 2024

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
viem (esm) 56.84 KB (0%) 1.2 s (0%) 6.8 s (+47.15% 🔺) 7.9 s
viem (cjs) 68.23 KB (0%) 1.4 s (0%) 7.9 s (-15.58% 🔽) 9.3 s
viem (minimal surface - tree-shaking) 3.3 KB (0%) 67 ms (0%) 126 ms (-39.19% 🔽) 193 ms
viem/accounts 73.91 KB (0%) 1.5 s (0%) 6.7 s (+90.12% 🔺) 8.2 s
viem/accounts (tree-shaking) 19.2 KB (0%) 384 ms (0%) 1.8 s (-12.32% 🔽) 2.2 s
viem/actions 41.78 KB (0%) 836 ms (0%) 6.2 s (+144.89% 🔺) 7 s
viem/actions (tree-shaking) 318 B (0%) 10 ms (0%) 134 ms (+58.77% 🔺) 144 ms
viem/chains 27.96 KB (0%) 560 ms (0%) 1.5 s (-65.94% 🔽) 2.1 s
viem/chains (tree-shaking) 324 B (0%) 10 ms (0%) 78 ms (-23.69% 🔽) 88 ms
viem/chains/utils 1.01 KB (0%) 21 ms (0%) 42 ms (-84.87% 🔽) 62 ms
viem/ens 41.74 KB (-0.09% 🔽) 835 ms (-0.09% 🔽) 4.1 s (+14.87% 🔺) 5 s
viem/ens (tree-shaking) 18.42 KB (+0.06% 🔺) 369 ms (+0.06% 🔺) 4.5 s (+27.7% 🔺) 4.9 s

Copy link

codecov bot commented May 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.90%. Comparing base (4496411) to head (db22244).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2307      +/-   ##
==========================================
- Coverage   96.60%   92.90%   -3.71%     
==========================================
  Files         675      644      -31     
  Lines       57392    52672    -4720     
  Branches     2439     1990     -449     
==========================================
- Hits        55443    48934    -6509     
- Misses       1919     3692    +1773     
- Partials       30       46      +16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +70 to +96
test('args: value', () => {
// payable function
simulateContract(clientWithAccount, {
abi: baycContractConfig.abi,
address: '0x',
functionName: 'mintApe',
args: [69n],
value: 5n,
})

// payable function (undefined)
simulateContract(clientWithAccount, {
abi: baycContractConfig.abi,
address: '0x',
functionName: 'mintApe',
args: [69n],
})

// nonpayable function
simulateContract(clientWithAccount, {
abi: baycContractConfig.abi,
address: '0x',
functionName: 'approve',
// @ts-expect-error
value: 5n,
})
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding more coverage for GetValue

@@ -309,10 +309,10 @@ export type GetValue<
_Narrowable extends boolean = IsNarrowable<TAbi, Abi>,
> = _Narrowable extends true
? TAbiFunction['stateMutability'] extends 'payable'
? { value?: NoUndefined<TValueType> | undefined }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NoUndefined is necessary anymore as the property is marked as optional and is a union with undefined.

@@ -309,10 +309,10 @@ export type GetValue<
_Narrowable extends boolean = IsNarrowable<TAbi, Abi>,
> = _Narrowable extends true
? TAbiFunction['stateMutability'] extends 'payable'
? { value?: NoUndefined<TValueType> | undefined }
? { value?: NoInfer<TValueType> | undefined }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We add NoInfer so that TValueType does not get inferred based on the property value.

Before

writeContract({
  functionName: 'nonpayable',
  value: '123',
  // ^? value (property): string
})
writeContract({
  functionName: 'payable',
  value: 123n,
  // ^? value (property): bigint
})

After

writeContract({
  functionName: 'nonpayable',
  value: '123',
  // ^? value (property): undefined
})
writeContract({
  functionName: 'payable',
  value: 123n,
  // ^? value (property): bigint | undefined
})

Might not look like a lot, but downstream this causes an issue with type transformations.

: TAbiFunction['payable'] extends true
? { value?: NoUndefined<TValueType> | undefined }
: { value?: never | undefined }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never | undefined becomes undefined so removing it so the compiler can have an extra vacation

@tmm tmm merged commit 15d4022 into main May 24, 2024
21 of 27 checks passed
@tmm tmm deleted the tmm/fix-ts-value branch May 24, 2024 20:10
@github-actions github-actions bot mentioned this pull request May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant