-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Conversation
|
packages/hooks/src/utils/index.ts
Outdated
@@ -1,3 +1,3 @@ | |||
export function isFunction(obj: any): obj is Function { | |||
export function isFunction<T>(obj: any): obj is T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不改应该是最好的吧。没理解为什么要加泛型呢。
There was a problem hiding this comment.
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已经提供了一个类似的方法了,所以这个贡献者应该是想去掉重复代码而已吧,我猜的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里应该把泛型去掉,这里的代码是没问题的。
然后把 createUseLocalStorateState 那边的函数替换下就好了~
There was a problem hiding this comment.
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导入,不需要重新定义
There was a problem hiding this comment.
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)
就可以自由传入自定义的参数类型
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useStorageState 里面直接改成 isFunction(value)
就好拉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hooks/packages/hooks/src/createUseStorageState/index.ts
Lines 69 to 74 in 098a914
if (isFunction<IFuncUpdater<T>>(options?.defaultValue)) { | |
return options?.defaultValue(); | |
} | |
return options?.defaultValue; | |
} | |
} else if (isFunction<IFuncUpdater<T>>(value)) { |
由于这两个地方使用了这个传参,所以我的想法是使用泛型,然后泛型默认给的是
Function
,不知道这个想法是否可以,望您指教一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hooks/packages/hooks/src/createUseStorageState/index.ts
Lines 69 to 74 in bde92f5
if (isFunction<IFuncUpdater<T>>(options?.defaultValue)) { return options?.defaultValue(); } return options?.defaultValue; }
@brickspert 应该是这里吧 我觉得 所以他才要写泛型
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我来改下~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
明白了~~~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
[English Template / 英文模板]
🤔 这个变动的性质是?
💡 需求背景和解决方案
utils/index.ts
已经定义好isFunction
函数,问题一:ts类型推断直接写为is Function
。问题二: 其他文件重复定义该函数,造成代码冗余function<T> isFunction(obj:any):obj is T
代替function isFunction(obj:any):obj is function
;问题二解决:删除重复定义的,从utils/index
中导入该函数📝 更新日志
isFunction
requires passing in the type of the parameterobj
isFunction
需要传入判断obj
参数对应的类型☑️ 请求合并前的自查清单