Flow: Flow: object literal. This type is incompatible with the expected param type of object type

Created on 17 Oct 2017  路  5Comments  路  Source: facebook/flow

Here is the sample

type Props = {
    param1: Object,
    param2: string,
    param3: [string] | [],
};

let var1 = { 'key': 'value'}

const params = {
            param1: var1,
            param2: 'param2',
            param3: Object.keys(var1),
        };

const func = ({ param1, param2, param3 }: Props) => {
  return JSON.stringify(param1) + param2 + JSON.stringify(param3);
};

func(params);
       ^ Error: Flow: object literal. This type is incompatible with the expected param type of object type 

What does it mean?
Thanks

Most helpful comment

Your type for param3, [string] | [], does not mean what you think it means. [string] is a tuple type meaning an array that contains exactly one element, which is a string. If you want an array type instead, you should write Array<string> or string[].

Similarly, [] is a tuple type that means an array that has zero elements. If you want your array of strings to support being empty, you don't need to put a union with []. Array<string> will already accept the empty array.

Flow raises an error because Flow knows that the global Object.keys function can return an array of strings. Flow's type for Object.keys isn't smart enough to look at the value of var1 and see that it's an object with only one enumerable own property; it thinks that the argument to Object.keys could have any number of properties.


By the way, I think the Flow GitHub issues are only meant to be for bug reports or feature requests. If you're having trouble understanding an error message and want help, asking a question on Stack Overflow and giving it the flowtype tag should get you quicker help.

All 5 comments

Your type for param3, [string] | [], does not mean what you think it means. [string] is a tuple type meaning an array that contains exactly one element, which is a string. If you want an array type instead, you should write Array<string> or string[].

Similarly, [] is a tuple type that means an array that has zero elements. If you want your array of strings to support being empty, you don't need to put a union with []. Array<string> will already accept the empty array.

Flow raises an error because Flow knows that the global Object.keys function can return an array of strings. Flow's type for Object.keys isn't smart enough to look at the value of var1 and see that it's an object with only one enumerable own property; it thinks that the argument to Object.keys could have any number of properties.


By the way, I think the Flow GitHub issues are only meant to be for bug reports or feature requests. If you're having trouble understanding an error message and want help, asking a question on Stack Overflow and giving it the flowtype tag should get you quicker help.

@roryokane Thanks for the explanation!
I've not understood syntax. I am a newbie in flow

Hi,

is there any way to disable this warning?

No, why would you like to do it?

@apsavin thanks for considering. I will get back to you why i thought it shouldn't be required ( a compultion ) like other properties. Need to recall.

Was this page helpful?
0 / 5 - 0 ratings