Javascript 访问嵌套流类型
我得到了一个“大”类型(在这个例子中是由继电器生成的),有很多嵌套正在进行。现在我需要访问嵌套类型 示例:我如何访问此处的“某物”Javascript 访问嵌套流类型,javascript,flowtype,Javascript,Flowtype,我得到了一个“大”类型(在这个例子中是由继电器生成的),有很多嵌套正在进行。现在我需要访问嵌套类型 示例:我如何访问此处的“某物” /*@flow*/ 嵌套类型={ anArray:$ReadOnlyArray } 类型nestedString=$PropertyType< $PropertyType, “有东西” >; 常量文本:nestedString=“测试” 链接到Flow REPL(需要切换到0.64,因为0.65目前不工作): 我将其分为几种类型,以便更容易理解: type ne
/*@flow*/
嵌套类型={
anArray:$ReadOnlyArray
}
类型nestedString=$PropertyType<
$PropertyType,
“有东西”
>;
常量文本:nestedString=“测试”
链接到Flow REPL(需要切换到0.64,因为0.65目前不工作):
我将其分为几种类型,以便更容易理解:
type nested = {
anArray: $ReadOnlyArray<{something: "BLA" | "TEST"}>
}
type ArrayType = $PropertyType<nested, 'anArray'>;
type ExtractArrayItem = <T>($ReadOnlyArray<T>) => T;
type ArrayItemType = $Call<ExtractArrayItem, ArrayType>;
type nestedString = $PropertyType<ArrayItemType, 'something'>;
const text: nestedString = "TEST";
嵌套类型={
anArray:$ReadOnlyArray
}
类型ArrayType=$PropertyType;
类型ExtractArrayItem=($ReadOnlyArray)=>T;
类型ArrayItemType=$Call;
type nestedString=$PropertyType用于提取数组项类型。实际上,您可以使用较新的方法来执行此操作,只需较少的步骤(分解为类似的部分,如:
嵌套类型={
anArray:$ReadOnlyArray
}
类型ArrayType=$PropertyType;
类型ArrayItemType=$ElementType
类型nestedString=$PropertyType;
常量文本:nestedString=“TEST”;
相当冗长,但它确实起到了作用!很好!因此基本上,$PropertyType
可以被各地的$ElementType
取代?你可以。关于$ElementType
有一些问题;特别是对于联合体:。请注意,这取决于你的需要。
type nested = {
anArray: $ReadOnlyArray<{something: "BLA" | "TEST"}>
}
type ArrayType = $PropertyType<nested, 'anArray'>;
type ExtractArrayItem = <T>($ReadOnlyArray<T>) => T;
type ArrayItemType = $Call<ExtractArrayItem, ArrayType>;
type nestedString = $PropertyType<ArrayItemType, 'something'>;
const text: nestedString = "TEST";
type nested = {
anArray: $ReadOnlyArray<{something: "BLA" | "TEST"}>
}
type ArrayType = $PropertyType<nested, 'anArray'>;
type ArrayItemType = $ElementType<nested, number>
type nestedString = $PropertyType<ArrayItemType, 'something'>;
const text: nestedString = "TEST";