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

185
Visualizações
Type hints of flow not stripped by babel

I have a React.JS project which uses a custom 'theme' with UI components. This theme also provides build scripts (webpack config, babel configs, etc.).

I want to start using Flow in this project. I installed the needed npm packages and added flow to babel's presets, then I added props = {mytestprop: string} to one of my React` classes.

Webpack compiled my code successfully, but the type hints were not stripped! Of course, the browser was not able to execute this code - when I try to run it, it raisesReferenceError: string is not defined.

The current list of presets from .babelrc is: ["es2015", "react", "stage-2", "flow"]. I'm sure that this is the actual list used by babel because if I delete any of the first 3 presets, compilation fails.

Do you have any ideas on what could lead to this behavior when stripping Flow types?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It's not that type annotations are not being stripped. It's that { mytestprop: string } is not a valid type annotation on the right-hand side of an assignment because it clashes with the syntax for defining an object.

Specifically, when Flow's parser sees the statement { mytestprop: string } it will interpret this as an attempt to create an object with a field named mytestprop with its value set to the value of the variable string, so it will leave the statement alone as it is, and you'll get the error you've seen in the browser.

The correct way to type object declarations is to type the left-hand side of the declaration.

For instance,

let myProps: { myTestProp: string } = { myTestProp: "testProp" };

if you aren't declaring your props separately, you could declare a custom type:

type myPropType = { myTestProp: string }
// ...
const myComponent = (props: myPropType) => //render your component

Since the type statement is exclusive to Flow and not a valid JavaScript statement, it will be stripped correctly.

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