Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda