Skip to content

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

Open

Description

@insonifi

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

Activity

  1. gkz commented on Aug 16, 2023

    @gkz
    Member

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

  2. insonifi commented on Aug 16, 2023

    @insonifi
    Author

    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.

  3. gkz commented on Aug 16, 2023

    @gkz
    Member

    empty is a subtype of all types including interface {}

    I agree what you are doing is what seems intuitive however

  4. insonifi commented on Aug 16, 2023

    @insonifi
    Author

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

  5. gkz commented on Aug 16, 2023

    @gkz
    Member

    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.

  6. added
    Typing: completeness
    and removed on Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Typing: completenessTyping: destructorsbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions