Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow type for optional field in an object

Tags:

In the following code (Try Flow):

type Response = {
    err: ?string;
    data: Object;
}
function length(x): Response {
  return { data : {} };
}

length(10)

I made err optional, but still get an error:

Property err not found in object literal

like image 457
bsr Avatar asked May 11 '16 14:05

bsr


1 Answers

This is the correct syntax for an optional property:

type Response = {
    err?: string;
    data: Object;
}

Demo

The syntax you tried to use (err: ?string) is a Maybe type, which means the err key should be in the object and can have type string, null, or void (undefined).

like image 93
Andrey Avatar answered Sep 22 '22 15:09

Andrey