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

176
Views
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 answers
Answer question

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 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!