A
A
Andrey2019-09-20 19:22:15
React
Andrey, 2019-09-20 19:22:15

Flow: why does it swear at an optional property in an object?

Sample code at the link
What's happening there. The map and renderItem functions operate on Item objects, which must have a "name" property. I am passing an array of objects of type User to this function, which has a required "name" property, but there are other fields as well. I am getting an error.
Although the documentation says that there should not be an error. That objects with additional properties are safe to use .
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
i1yas, 2019-09-20
@f-end

I've run into this "problem" too. The trick is that flow considers the worst-case scenarios.
Here is an answer on SO
. This type allows you to do this kind of thing:

function map(items: Array<Item>) {
  ...
  items.push({name: "Vasya"});
  ...
}
map(list)

We push an object of type Items, locally everything is OK, only we push it into an array of type Array<Users>, which has a more strict type. Therefore, the flow swears.
Decision:
function map<T: {name: string}>(items: Array<T>) {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question