fix: useMemoizedFn return a plain function type #1470
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[中文版模板 / Chinese template]
🤔 This is a ...
💡 Background and solution
我们考察如下代码
TypeScript 推导出
memoizedFn.hello
为不可空的string
, 但实际访问则是undefined
。如果
memoizedFn.hello
还是个对象,继续使用.
运算符,那么还会有更可怕的异常发生。hooks/packages/hooks/src/useMemoizedFn/index.ts
Lines 20 to 22 in cbc8f8f
原来的代码,由于使用的类型强制转换,导致类型推导出现错误,我们这里的返回值类型,应该剔除 fn 对象中的所有属性,而只保留其函数的部分。
因此我们可以定义一个 ts type,它的泛型参数可以接受任意函数对象,然后转换成一个普通的函数
📝 Changelog
☑️ Self Check before Merge