首页 > 解决方案 > Passing object to method that will must have one specific property

问题描述

I have a utility method like this:

function testUtil(row) {
    ...
    const i = row.Index;
    ...
}

How to set the type of 'row' anything but it must have Index property.

标签: typescript

解决方案


您可以声明一个接口并创建该类型的行。

interface IRowType {
 index: number;
}

function testUtilRow(row: IRowType){
  const index = row.index;
}

推荐阅读