TypeScript 将具有解构参数的函数进行转换后,参数名称会丢失,

yks3o0rb  于 8个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(106)

建议

🔍 搜索词

元组, 标签, 参数, 解构

✅ 可实现性检查清单

我的建议满足以下准则:

  • [x] 这不会对现有的TypeScript/JavaScript代码造成破坏性的更改
  • [ x] 这不会改变现有JavaScript代码的运行时行为
  • [ x] 这可以在不根据表达式的类型发出不同的JS的情况下实现
  • [x] 这不是一个运行时特性(例如库功能,具有JavaScript输出的非ECMAScript语法,JS的新语法糖等)
  • [ x] 这个特性会与TypeScript's Design Goals的其他部分一致。

⭐ 建议

当使用元组类型捕获函数参数时,如果任何参数被解构,则会丢失参数名称,这会导致在尝试确定原始类型参数顺序时出现不太理想的情况。

📃 动机示例

  1. // trivial exampled
  2. const transform = <T, U extends unknown[]>(
  3. factory: (...args: U) => T
  4. ) : ((...args: U) => T) => factory
  5. const transformed = transform(({ test } : { test: string }, b: string) : string => `${test}:`${b}`)
  6. /*
  7. The intellisense for the transformed function shows args_0, args_1, the first
  8. argument clearly has no name, however the second does but is still not shown. If
  9. all arguments are named then everything works as expected. I realize this
  10. is a fairly trivial example, however, it would significantly improve usage of
  11. transformed functions, particularly if those function have multiple primitive
  12. arguments whose order could be confused without names.
  13. Additionally I understand the potential for argument name collision, e.g.
  14. if argument two was for some reason called args_0, however, I believe this
  15. case is unlikely and also could be fixed with a numeric suffix which would
  16. still retain the intent.
  17. */

💻 用例

wko9yo5t

wko9yo5t1#

元组标签是一种尽最大努力的保留;如果我们得到一个不是超级复杂的修复,我们会考虑它,但我们不希望采取任何会增加大量代码的东西。

相关问题