Gajus
Gajus

Reputation: 73808

How to define a Flow type for an object that must have at least one of the properties?

I have the following object declaration:

type QueryQuantifierType = {
  +max?: number,
  +min?: number,
  +multiple?: boolean
};

It permits:

{
  min: 1
}

{
  max: 1
}

{
  max: 1,
  min: 1
}

{
  min: 1,
  multiple: true
}

{
  max: 1,
  multiple: true
}

These are valid values.

However, the problem is that it also permits an empty object.

How to define a Flow type for an object that must have at least one of the properties?

Upvotes: 4

Views: 1365

Answers (1)

Gajus
Gajus

Reputation: 73808

The only way I have figured out how to do this is using a union of exact objects:

type QuantifierType =
  {|
    +max: number,
    +min?: number,
    +multiple?: boolean
  |} |
  {|
    +max?: number,
    +min: number,
    +multiple?: boolean
  |} |
  {|
    +max?: number,
    +min?: number,
    +multiple: boolean
  |};

Upvotes: 4

Related Questions