When you declare a type
function getName(thing: {name?: string}): string {
return thing.name || 'unknown';
}
this means that the type will be used for your function
function getName(thing: {name?: string}): string {
thing.name = undefined;
return "anything";
}
because you said it was name
allowed to be undefined
.
Given that
const thing: {name: string} = {name: 'bob'};
getName(thing);
will break type safety, the value will no longer be a string.
. , , , name
.
function getName(thing: { +name?: string }): string {
return thing.name || 'unknown';
}
Flow , name
. , {name: string}
, .