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

refactor(isFunction): refactor isFunction duplicate definition #1531

Merged
merged 3 commits into from
Mar 31, 2022
Merged

refactor(isFunction): refactor isFunction duplicate definition #1531

merged 3 commits into from
Mar 31, 2022

Conversation

coder-th
Copy link
Contributor

[English Template / 英文模板]

🤔 这个变动的性质是?

  • 新特性提交
  • 日常 bug 修复
  • 站点、文档改进
  • 演示代码改进
  • TypeScript 定义更新
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

💡 需求背景和解决方案

  1. utils/index.ts已经定义好isFunction函数,问题一:ts类型推断直接写为is Function。问题二: 其他文件重复定义该函数,造成代码冗余
  2. 问题一解决:使用function<T> isFunction(obj:any):obj is T代替function isFunction(obj:any):obj is function;问题二解决:删除重复定义的,从utils/index中导入该函数

📝 更新日志

语言 更新描述
🇺🇸 英文 Using isFunction requires passing in the type of the parameter obj
🇨🇳 中文 使用isFunction需要传入判断obj参数对应的类型

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • Changelog 已提供或无须提供

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@@ -1,3 +1,3 @@
export function isFunction(obj: any): obj is Function {
export function isFunction<T>(obj: any): obj is T {
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
Contributor

Choose a reason for hiding this comment

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

@brickspert 我猜应该是 在createUseLocalStorateState有一个自定义的isFunction,因为在utils的index已经提供了一个类似的方法了,所以这个贡献者应该是想去掉重复代码而已吧,我猜的

Copy link
Collaborator

Choose a reason for hiding this comment

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

这里应该把泛型去掉,这里的代码是没问题的。

然后把 createUseLocalStorateState 那边的函数替换下就好了~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@brickspert @PlayGuitar-CoderQ 是的,正如这个老哥所言,我想把这个isFunction直接从utils导入,不需要重新定义

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@brickspert 我觉得这样改会更好,给他默认的Function
export function isFunction<T = Function>(obj: any): obj is T { return typeof obj === 'function'; }
如果使用这个方式,这样在useStorageState这个函数中涉及到的
isFunction<IFuncUpdater<T>>(value)就可以自由传入自定义的参数类型

Copy link
Collaborator

Choose a reason for hiding this comment

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

useStorageState 里面直接改成 isFunction(value) 就好拉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if (isFunction<IFuncUpdater<T>>(options?.defaultValue)) {
return options?.defaultValue();
}
return options?.defaultValue;
}

} else if (isFunction<IFuncUpdater<T>>(value)) {

由于这两个地方使用了这个传参,所以我的想法是使用泛型,然后泛型默认给的是Function,不知道这个想法是否可以,望您指教一下

Copy link
Contributor

Choose a reason for hiding this comment

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

if (isFunction<IFuncUpdater<T>>(options?.defaultValue)) {
return options?.defaultValue();
}
return options?.defaultValue;
}

@brickspert 应该是这里吧 我觉得 所以他才要写泛型

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
Contributor Author

Choose a reason for hiding this comment

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

明白了~~~

Copy link
Collaborator

@brickspert brickspert left a comment

Choose a reason for hiding this comment

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

LGTM

@brickspert brickspert merged commit d6ca2b6 into alibaba:master Mar 31, 2022
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.

4 participants