0

I'm working with typescript and I ran into an unfamiliar notation:

interface IDetailsListGroupedExampleItem {
  key: string;
  name: string;
  color: string;
}

...
...
...

private _onRenderColumn(item: IDetailsListGroupedExampleItem, index: number, column: IColumn) {
  const value =
    item && column && column.fieldName
      ? item[column.fieldName as keyof IDetailsListGroupedExampleItem] || ''
      : '';

  return <div data-is-focusable={true}>{value}</div>;
}

Specifically, what does this line mean item[column.fieldName as keyof IDetailsListGroupedExampleItem] ?

Item is not an array, so I don't understand the usage of square brackets in this case. Is it a way to access a specific property of the item object?

Vas
  • 2,014
  • 3
  • 22
  • 38
  • 1
    I don't think this question is a duplicate of the mentioned question. Anyways, the `keyof` operator is used to refer to the public keys of a type. In your example `column.fieldName as keyof IDetailsListGroupedExampleItem` says that `column.fieldName` must resolve to one of the public keys of the `IDetailsListGroupedExampleItem` interface. Check this [link](https://www.typescriptlang.org/docs/handbook/advanced-types.html) for more information. – guzmonne Jul 15 '19 at 22:54
  • @guzmonne thank you! that helps decypher the other part of the notation – Vas Jul 16 '19 at 00:10

0 Answers0