Flow: ALL properties are optional - but Flow complains about assigning an empty object

Created on 19 Jan 2018  路  6Comments  路  Source: facebook/flow

Flow Try Link

Code:

type OptionsType = {|
    optional1?: number,
    optional2?: boolean,
    optional3?: boolean
|};

function demo (options?: OptionsType = {}): void {
    // ...
};

Error message:

7: function demo (options?: OptionsType = {}): void {
                                          ^ object literal. Inexact type is incompatible with exact type
7: function demo (options?: OptionsType = {}): void {
                            ^ OptionsType

It works when I use { .... } instead of {| .... |} for the type definition, but I don't want that because then using a non-existent property would work too (example: demo({foo:1}); would not cause an error message either). I declared ALL properties as optional, therefor assigning an empty object should not cause an error.

duplicate

Most helpful comment

This looks to be related to #4582

The same workaround should work for you though. It's not _exactly_ exact, but it gets the job done in most situations:

type OptionsType = {
    optional1?: number,
    optional2?: boolean,
    optional3?: boolean,
    [any]: empty
};

All 6 comments

Does an inexact type and $Shape<T> satisfy your requirements: Try Flow?

@idiostruct Good to know there is a workaround! However, the original construct should work, the type definition is fulfilled by an empty object.

This looks to be related to #4582

The same workaround should work for you though. It's not _exactly_ exact, but it gets the job done in most situations:

type OptionsType = {
    optional1?: number,
    optional2?: boolean,
    optional3?: boolean,
    [any]: empty
};

@jcready

The same workaround should work for you though.

See my previous comment :-) My focus is on the big report, I can always find some workaround - even if it is to go through any or do something completely different. I know a fix is unlikely given the huge backlog, but here is the report anyway.

This is a duplicate bug report of #4582

This probably can be closed.
/cc @vkurchatkin

Was this page helpful?
0 / 5 - 0 ratings

Related issues

doberkofler picture doberkofler  路  3Comments

damncabbage picture damncabbage  路  3Comments

jamiebuilds picture jamiebuilds  路  3Comments

ctrlplusb picture ctrlplusb  路  3Comments

funtaps picture funtaps  路  3Comments