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