4

In Flow Types :

export type GroupType = {
  options: OptionsType,
  [string]: any,
};

What does [string]: any mean ?

Edit :

Thank you for your responses.

How do I must understand this piece of code ?

import type { GroupType } from './types';
const formatGroupLabel = (group: GroupType): string => group.label;

For me formatGroupLabel is a function that takes group as a parameter and return group.label. But I don't understand why there is : string in front of (group: GroupType). May be there is no link with my first question.

nick zoum
  • 6,639
  • 5
  • 26
  • 63
Mickaël Gauvin
  • 597
  • 6
  • 15

1 Answers1

4

It's an index property, it means that if you try to get a property whose key is a string, the value will be of type any. Documentation

It's quite common to use it like this to avoid confusion (but both ways are valid):

export type GroupType = {
  options: OptionsType,
  [index: string]: any
};

So you can use it for objects like:

/** @type {GroupType} */
var type = {
  options: {},
  a: 1,
  b: "foo",
  c: function fooBar() {}
};

console.log(typeof type["a"]);
console.log(typeof type["b"]);
console.log(typeof type["c"]);

For the second part, const formatGroupLabel = (group: GroupType): string => group.label; is a function that accepts a GroupType, returns a string and it's body is return group.label. It would have the following format in JavaScript:

/** @type {(group: GroupType) => string} */
const formatGroupLabel = (group) => {
  return group.label;
};
nick zoum
  • 6,639
  • 5
  • 26
  • 63