flow icon indicating copy to clipboard operation
flow copied to clipboard

Partial type does not accept plain empty object literal as a default

Open insonifi opened this issue 2 years ago • 5 comments

Flow version: 214

Expected behavior

The partial type also accepts object literal as a default value since it is an extreme case where all properties were omitted <T>(Partial<T> = {}) => {}

Actual behavior

Flow throws an incompatible type error because the object literal is incompatible with {...T}. It can be circumvented by casting an any type to the object.

<T>(Partial<T> = ({}: any)) => {}

Flow try demo

insonifi avatar Aug 16 '23 12:08 insonifi

Technically you can pass in empty as the type argument, and Partial<empty> is empty, and {} is not a valid value for empty

gkz avatar Aug 16 '23 15:08 gkz

The documentation:

This utility converts all of an object or interface's named fields to be optional, while maintaining all the object's other properties (e.g. exactness, variance).

I understand that there could only be an indexed type. Hence empty cannot be used for a partial type.

insonifi avatar Aug 16 '23 15:08 insonifi

empty is a subtype of all types including interface {}

I agree what you are doing is what seems intuitive however

gkz avatar Aug 16 '23 16:08 gkz

What do you think is the idiomatic way to handle it, then?

insonifi avatar Aug 16 '23 16:08 insonifi

There is no way to do that currently. What I'm saying is that what we are currently doing is technically correct and safe, however it obviously doesn't align with what people intuitively think should happen, so we should think about it.

gkz avatar Aug 16 '23 17:08 gkz