使用react应用打字稿

时间:2019-11-07 11:45:49

标签: typescript

<Test>
      {({
        direction,
        filteredItems,
        onItemSelectAll,
        onItemSelect,
        selectedKeys: listSelectedKeys,
        disabled: listDisabled,
      }) => {
        const columns = direction === 'left' ? leftColumns : rightColumns;
.......

在我的情况下,我使用打字稿,因此当我尝试运行代码时,对于此部分代码,将显示错误:Binding element 'direction' implicitly has an 'any' type 所以,现在我想现在如何应用打字稿,因为我试图写direction:any但我得到了'any' is declared but its value is never read.  在这种情况下如何应用打字稿?我的意思是directiondisabled之间的距离?

2 个答案:

答案 0 :(得分:1)

您是否要破坏函数的参数?如果可以,我认为在TypeScript中执行此操作的方式如下:

interface Args {
  direction: any
  selectedKeys: listSelectedKeys
  disabled: listDisabled
}

({ direction, selectedKeys, disabled }: Args) => {
  // on hover, direction is of 'any' type
  console.log(direction)
}

答案 1 :(得分:0)

我想您正在尝试为解构对象属性指定类型,如果是这种情况,请尝试

<Test>
      {({
    direction,
    filteredItems,
    onItemSelectAll,
    onItemSelect,
    selectedKeys: listSelectedKeys,
    disabled: listDisabled
  }: {
    direction: any;
    filteredItems: any;
    onItemSelectAll: any;
    onItemSelect: any;
    selectedKeys: { listSelectedKeys: any };
    disabled: { listDisabled: any };
  }) => { const columns = direction === 'left' ? leftColumns : rightColumns;

代替any,只需在此处使用所需的任何类型即可。