【问题标题】:TypeScript: How to set type of destructured object?TypeScript:如何设置解构对象的类型?
【发布时间】:2018-12-19 05:18:27
【问题描述】:

问题

比如参数有如下类型{ component: any; element: ElementRef; },我只想要其中的元素:

onTextBoxInit({ element }) { }

问题:如何设置这个解构元素的类型?

试过

我想到了这样的东西:onTextBoxInit({ element: ElementRef }) { },但这不起作用。

【问题讨论】:

  • 唯一的选择是设置参数onTextBoxInit({ element }: {element: ElementRef })的类型
  • @TitianCernicova-Dragomir 哦,哈哈,谢谢,这就是答案,请提交:)
  • 我怎么没想到:s
  • 已提交,根据您的问题,我认为这不是您要找的 :-)

标签: typescript typescript-typings


【解决方案1】:

解构表达式后需要指定参数的类型

 onTextBoxInit({ element }: {element: ElementRef }){} 

【讨论】:

  • 在解构数组中设置解构对象的类型:([{todoId}, _]: [{todoId: string}, void]) => todoId(例如:当使用 RxJS 的 combineLatest 运算符与参数和其他一些主题时)。
猜你喜欢
  • 2021-12-01
  • 2022-12-11
  • 2021-10-27
  • 2021-12-13
  • 2022-08-19
  • 2020-12-07
  • 2017-02-02
  • 2020-10-03
相关资源
最近更新 更多