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.
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; };
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.