Flow export a function using map

282 Views Asked by At

I would like a function that get values from an object, but flow complains when I try to export it. I cannot figure out how to write the right annotations.

type RowType = {|foo:string|};
type ListType = {[id:string]:RowType};

const objList: ListType  = {
  someId:{foo:"someValue"}
}

const values1 = (obj) => {
  return Object.keys(obj).map(key => obj[key]);
}

values1(objList).forEach((row) => {
  row.foo; //work as expected
  row.bar; //raise an error as expected
});


export const values2 = (obj) => {
  //Missing type annotation for `obj`
  return Object.keys(obj).map(key => obj[key]);
  //Missing type annotation for `U`. `U` is a type parameter declared in function type [1]
  // and was implicitly instantiated at call of method `map` [2].
}

values2(objList).forEach((row) => {
  row.foo; //expect to work
  row.bar; //expect to raise an error
});
1

There are 1 best solutions below

0
B. Beauvais On

Find how to fix it

type RowType = {|foo:string|};
type ListType = {[id:string]:RowType};

const objList: ListType  = {
  someId:{foo:"someValue"}
}

export const values = <Row>(obj: {[string]:Row}):Array<Row> => {
  return Object.keys(obj).map(key => obj[key]);
}

values(objList).forEach((row) => {
  row.foo; //work
  row.bar; //raise an error
});