Reputation: 145
Here is the exemple:
/* @flow */
function a (params: {p1: string, p2?: string}): void {}
function b (): {p1: string} {
return {p1: 'string'};
}
var bResult = b();
a(bResult);
And I got an flow error
3: function a (params: {p1: string, p2?: string}): void {}
^ property `p2`. Property not found in
11: a(bResult);
^ object type
Why this error happens?
Why signature {p1: string}
not is part of {p1: string, p2?: string}
?
Upvotes: 1
Views: 144
Reputation: 4086
The type {p1: string, p2?: string}
means that if a value of this type has a p2
property, it must be a string
. Flow's width subtyping means that the b
function could return {p1: 'foo', p2: 6}
. Basically, if you have something of type {p1: string}
you know it has a p1
property but you don't know anything about its other properties.
Because of this, if Flow allowed you to take something of type {p1: string}
and pass it to the a
function which expects the type {p1: string, p2?: string}
, the a
function could check if the p2
property exists, and if so it could assume that it is a string
. However, it could actually be a number
as detailed above. This will break type safety.
Upvotes: 2