首页 > 解决方案 > 在提供的参数上解构对象

问题描述

我想不出一种从提供的参数中解构对象的方法:

type PossibleStateProperty = 'hello' | 'world'

type State = {
  hello: 'welcome',
  world: 'earth'
}

handleError = (name: PossibleStateProperty) => {
   const { [name] } = this.state
}

我将state根据提供的函数参数解构对象属性。

const { [name] } = this.state

可悲的是无效

标签: javascriptreactjstypescriptecmascript-6destructor

解决方案


推荐阅读