The following code
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];
I want to loop through an array of Value objects and get the name value of all my objects, but I'm getting this error:
9: const arr: Array<Value> = [myNumber, myString];
^ Cannot use `Value` [1] without 1 type argument.
References:
1: type Value<T> = {|
^ [1]
Any ideas how to fix this without using Value<any>? I'm using flow strict
Flow Playground link
Update In latest version (tested with 0.98.0) flow is able to infer the types properly, so explicit annotation is no longer required. Just go with:
const arr = [myNumber, myString];
Older versions:
In flow you could use Existential Type (*)
An existential type is used as a placeholder to tell Flow to infer the type
const arr: Array<*> = [myNumber, myString];
Playground
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With