Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Flowtype: conditionally required type

I have the following code in my React app:

import isRequiredIf from 'react-proptype-conditional-require'

Auth.propTypes = {
  children: PropTypes.any,
  step: PropTypes.number,
  steps: isRequiredIf(PropTypes.number, props => props.hasOwnProperty('step')),
  footer: PropTypes.any
}

Here steps is required only if step is specified.

I'm migrating my React PropTypes to Flow. Can I somehow achieve the same behavior with Flow?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This can't be done with Flow in this form. Since PropTypes are checked at runtime, and not statically, there can be more flexibility. If a static typechecker allowed this sort of thing, it would be undecidable.

However, you could do something like this:

type Props = {
  children: any,
  // not sure about this name, but you get the idea
  step: ?{
    step: number,
    steps: number,
  },
  footer: any,
}

Or, if you can have steps but not step:

type Props = {
  children: any,
  // not sure about this name, but you get the idea
  step: ?{
    step?: number,
    steps: number,
  },
  footer: any,
}

Obviously, neither of these options is quite as concise as what you have, and it will require changing the code that produces and consumes these values. Unfortunately there is sometimes a price to be paid in order to gain the safety of a static typechecker.

Another option is to just leave them both optional:

type Props = {
  children: any,
  step?: number,
  steps?: number,
  footer: any,
}

This way, Flow won't enforce that steps exists if step exists. However, you won't have to change your other code, and there's nothing stopping you from adding a runtime check (which will be no worse than what you already have).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!