简体   繁体   中英

Flow type for optional field in an object

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

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM