Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lll000111 opened this issue Jan 19, 2018 · 6 comments

Comments

@lll000111
Copy link
Contributor

lll000111 commented Jan 19, 2018

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.

@ghost
Copy link

ghost commented Jan 19, 2018

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

@lll000111
Copy link
Contributor Author

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

@jcready
Copy link
Contributor

jcready commented Jan 19, 2018

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
};

@lll000111
Copy link
Contributor Author

lll000111 commented Jan 19, 2018

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

@jcready
Copy link
Contributor

jcready commented Jan 19, 2018

This is a duplicate bug report of #4582

@TrySound
Copy link
Contributor

This probably can be closed.
/cc @vkurchatkin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants