Answer the question
In order to leave comments, you need to log in
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
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)
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. function map<T: {name: string}>(items: Array<T>) {
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question