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: hooks missing return type #1200

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-lemons-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ant-design/web3': patch
---

fix: hooks missing return type
4 changes: 3 additions & 1 deletion packages/web3/src/hooks/useAccount.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { UniversalWeb3ProviderInterface } from '@ant-design/web3-common';

import useProvider from './useProvider';

export default function useAccount() {
const { account } = useProvider();
const { account } = useProvider() as UniversalWeb3ProviderInterface;
Copy link
Collaborator

Choose a reason for hiding this comment

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

image

好奇怪,为啥我这里看是有类型的。

另外感觉用 as 不太好,有问题的话最好也是通过补充 useProvider 的类型来解决。

Copy link
Collaborator

Choose a reason for hiding this comment

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

image

我看项目里面的 demo 这个类型是有的

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

要看打包后的 d.ts 文件,不用 as 指定一下类型,会导致编译结果不对:
image

Copy link
Collaborator

Choose a reason for hiding this comment

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

不有应该啊,总感觉有其它更好的办法,as 一般是用在那种本来就有可能有多种类型的情况下用来指定类型。但是这个的问题是类型没有推理出来,最好再研究一下

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

又改了一下,原因似乎是由于把 enum 和 type/interface 放到同一个文件里面了。导致后面的模块寻找类型的时候出错了。

我在本地试了下可以了,你那边最好也拉下来试一下。

Copy link
Collaborator

Choose a reason for hiding this comment

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

为啥 enum 和 type 放一起会有问题?可以研究下两种情况下构建出来的 type 文件有什么区别。

感觉这种问题一般是 ts 构建 .d.ts 文件的时候把一些类型吞掉了。

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

检查了下,编译出来的类型并没有丢失。

而且毕竟,这个文件中的类型很多地方都有用,如果丢失了,那么其他用到的地方应该都有问题,而事实是只发现这两个 hook 里面的类型有问题。

能想到的其他原因,例如“循环依赖”、“TS 编译时类型查找错误”,似乎都不能满足上面这一点。。。

Copy link
Collaborator

Choose a reason for hiding this comment

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

奇怪,那为啥分开写到一个单独的文件就好了

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

这种修复方式似乎是不对的,我今天再次执行 build 后,又出现了原问题。


return {
account,
Expand Down
4 changes: 3 additions & 1 deletion packages/web3/src/hooks/useConnection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { UniversalWeb3ProviderInterface } from '@ant-design/web3-common';

import useProvider from './useProvider';

export default function useConnection() {
const { connect, disconnect } = useProvider();
const { connect, disconnect } = useProvider() as UniversalWeb3ProviderInterface;

return {
connect,
Expand Down
Loading