下面的代码
type Value<T> = {|
name: string,
value: T,
|}
const myNumber: Value<number> = { name: 'number', value: 1 };
const myString: Value<string> = { name: 'string', value: 'foo' };
const arr: Array<Value> = [myNumber, myString];
我想遍历 Value
的数组对象并获得 name
我所有对象的值,但出现此错误:
9: const arr: Array<Value> = [myNumber, myString];
^ Cannot use `Value` [1] without 1 type argument.
References:
1: type Value<T> = {|
^ [1]
关于如何在不使用 Value<any>
的情况下解决此问题的任何想法?我正在使用 flow strict
心流乐园 link
最佳答案
更新 在最新版本(使用 0.98.0 测试)中,流程能够正确推断类型,因此不再需要显式注释。只需:
const arr = [myNumber, myString];
旧版本:
在流程中你可以使用Existential Type (*)
An existential type is used as a placeholder to tell Flow to infer the type
const arr: Array<*> = [myNumber, myString];
关于javascript - 使用泛型流类型值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53076390/